3

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.

2
  • Can you post a sample of how the string look like? To split string there are quite a few methods using regex and non-regex solutions Commented Nov 17, 2015 at 10:10
  • the string looks like a jsonArray. Commented Nov 17, 2015 at 10:33

1 Answer 1

10

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();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.