Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split json object from json array in java

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.

like image 880
Itsik Mauyhas Avatar asked Jul 05 '26 12:07

Itsik Mauyhas


1 Answers

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();
}
like image 115
ThomasThiebaud Avatar answered Jul 08 '26 00:07

ThomasThiebaud



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!