1

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.

2 Answers 2

1

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

4 Comments

Thanks.. But I mean the key can be without "
Ok, I doublechecked, according to the json specification its invalid, according to different implementations (eg.g javascript) it "might" be valid.
with your above code I can get the value of C, but how do I get the values of D, E , F , G ???
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 objects
0

Hope 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++)
{
... // 

}

5 Comments

Ok. But how do I traverse into the jsonobjects inside the array ?
@user1677986: Have updated my answer, please check if that has helped
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"}},
@user1677986: PLease check now
In my case C is an array and I want to also print other values like E, F, G and so on.

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.