I am new to JSON parsing and I would need your kind help to understand how to parse a JSON object inside an Array.
My JSON structure log is below:
{A:"text",B:"text",C:[{T:"Text",D:{E:"Text",F:{G:"time"}},H:{I:"200"},J:{K:{L:53,M:2.2},N:"Text"},P:{Q:"Time"}}]}
log is my input JSON as above.
JSONObject logJson = (JSONObject) JSONSerializer.toJSON( log );
String a = logJson.getString("A");
String b = logJson.getString("B");
Now question is how do I parse into the Array and get JSONObject. I am using net.sf API.
asked Nov 29, 2012 at 13:31
user1677986
611 gold badge1 silver badge7 bronze badges
2 Answers 2
First of all, I think your json format is invalid.
The "key" must stand in " as well e.g.
"A":"text"
You can get the "C" Array by using JSONArray jsonArray = logJson.getJSONArray("C");
On the array you can iterate:
for(int i = 0; i < jsonArray.length(); i++)
{
JSONObject obj = jsonArray.getJSONObject(i);
...
}
answered Nov 29, 2012 at 13:47
HashtagMarkus
1,66111 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
user1677986
Thanks.. But I mean the key can be without "
HashtagMarkus
Ok, I doublechecked, according to the json specification its invalid, according to different implementations (eg.g javascript) it "might" be valid.
user1677986
with your above code I can get the value of C, but how do I get the values of D, E , F , G ???
HashtagMarkus
The same way as before... If its an object
obj.getJSONObject("D"); if its an array obj.getJSONArray("D"); The structure of this json string is pretty awkward, so it's very inconvenient to access all JSON objectsHope this will help ::
JSONObject objonthwiseClaimJSON = JSONObject.fromString(strJSONMonthwiseClaim);
JSONArray objDateWiseClaimJSONArr = objonthwiseClaimJSON.getJSONArray("dateWiseClaimList");
for(int dtwclaim=0; dtwclaim<objDateWiseClaimJSONArr.length();dtwclaim++)
{
JSONObject objDateWiseClaimJSONObject =(JSONObject) objDateWiseClaimJSONArr.get(dtwclaim);
String dcnid = (String) objDateWiseClaimJSONObject.get("dcnid");
String remark = (String) objDateWiseClaimJSONObject.get("remark");
}
In your situation ::
JSONObject logJson = (JSONObject) JSONSerializer.toJSON( log );
String a = logJson.getString("A");
String b = logJson.getString("B");
JSONArray c = logJson.getJSONArray("C"); // If C is not an array, just a plain object
for(int i =0;i<c.length();i++)
{
... //
}
answered Nov 29, 2012 at 13:46
Sashi Kant
13.5k9 gold badges50 silver badges71 bronze badges
5 Comments
user1677986
Ok. But how do I traverse into the jsonobjects inside the array ?
Sashi Kant
@user1677986: Have updated my answer, please check if that has helped
user1677986
Ok... This helped me to understand retreving jsonobject inside array, but how do I get the json object, like in the example C:[{T:"Text",D:{E:"Text",F:{G:"time"}},
Sashi Kant
@user1677986: PLease check now
user1677986
In my case C is an array and I want to also print other values like E, F, G and so on.
lang-java