split json object from json array in java

浪子不回头ぞ 提交于 2021-02-10 04:54:15

问题


I am sending API call to a service that return a json array like this :

[Object, Object ....]

via my java http request. the resulat are stored in a string:

StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

I need to find a way to split this string to by json objects so each new string will contain only one object. Thanks.


回答1:


Instead of using the split function, you can convert your String to a JSONArray and then iterate throw the array

JSONArray jsonArray = new JSONArray(response.toString());
for(int i=0; i<jsonArray.length(); i++) {
    JSONObject jsonObject = jsonArray.getJSONObject(i);
    String jsonObjectAsString = jsonObject.toString();
}


来源:https://stackoverflow.com/questions/33754101/split-json-object-from-json-array-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!