Json Reponse With Repeated Json Objects Without Parent Json Arry
Above is the json response I am receiving from a url. There are repeated json objects in the response at the same level without a parent json array, I believe that these objects s
Solution 1:
How can be looped through and receive information in such scenario ?
You should use ITERATOR for this case .
FYI
Iterator is a way to traverse the data over the collection objects.
JSONObject jOBJECT = newJSONObject(success);
Iterator iteratorObj = jOBJECT.keys();
while (iteratorObj.hasNext())
{
String getJsonObj = (String)iteratorObj.next();
System.out.println("Key: " + Key + "------>" + getJsonObj); // 78,40,121,132
}
Solution 2:
Here is the detailed solution for such pattern of objects in the service response.
Solution 3:
this code maybe can help you:
try {
String data="";//this is you jsonJSONObject jsonObject=newJSONObject(data);
JSONArray messages = jsonObject.getJSONArray("message");//get a array//loop the arrat to outputfor (int i = 0; i < messages.length(); i++) {
JSONObject msg=messages.getJSONObject(i);
String id= msg.getString("id");
String username= msg.getString("username");
System.out.println("id:"+id+",username:"+username);
}
} catch (JSONException e) {
e.printStackTrace();
}
but,I advise you don't use this in your project, you can consider Gson
,use this you can transfer json to java model,and then it's easy to use.
Post a Comment for "Json Reponse With Repeated Json Objects Without Parent Json Arry"