My JSON string looks like this (contained in the sJSON
string variable):
[ {"id":284} ], [ {"name":John,"surname":Doe}, {"name":Jane,"surname":Doe} ]
I can parse the first array like this (using Java and importing org.json):
JSONArray arrJSON = new JSONArray(sJSON); JSONObject jsonGeneralData = new JSONObject(arrJSON.get(0).toString()); String sResult = jsonGeneralData.get("id").toString();
This returns the expected result, which is 284. I'm struggling to get the second array of elements and scroll through them. I am not sure if my JSON string is incorrect, or if I try to get the wrong path to it. Here is what I tried:
JSONObject jsonPersonData = new JSONObject(arrJSON.get(1).toString());
This, as I understand it, I canβt figure out how to iterate over individual elements inside the second array.
EDIT:
This line seems to only parse the first line in square brackets:
JSONArray arrJSON = new JSONArray(sJSON);
Or is JSON wrong (the same example as above), or doesn't it parse it correctly? I managed to solve the problem by splitting it into a string and putting them each in my own JSONArray
, but I don't think this is the best way to do something.
Mr. Smith
source share