0

This is my json data

 {
    "tag1":["haaai ","hello"],

    "tag2":[["haai1","haai2"],["hello1","hello2","hello3"]]
 }

I can successfully read array tag1 using my java code

 JSONArray msg = (JSONArray) jsonObject.get("tag1");    
 Iterator<String> iterator = msg.iterator();

 while (iterator.hasNext()) {    
        String text = iterator.next();    
        System.out.println(text);
 }

But, I can't read the tag2 array using the above method. Any idea?

1
  • You have two seperate objects in your json; you have to retreive them seperately. Or re-think your json data structure, i.e. with a root object. Commented Jul 8, 2015 at 7:24

1 Answer 1

3

You can't read using the same method because the format is different. You will have to use 2 loops instead.

 JSONArray msg = (JSONArray) jsonObject.get("tag2");    
 Iterator<JSONArray> iterator = msg.iterator();

 while (iterator.hasNext()) {
        Iterator<String> innerIterator = iterator.next().iterator();
        while (innerIterator.hasNext()) {
             String text = innerIterator.next();    
             System.out.println(text);
        }
 }
Sign up to request clarification or add additional context in comments.

1 Comment

its working fine. thanks for your valuable 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.