2

I have json string like this:

{  
    "2":{  
        "id":"2",
        "first":"3",
        "last":"2",
        "ilike":"1",
        "created_at":"2015-06-30 16:57:39",
        "liketo":"2",
        "firstname":"FirstName",
        "lastname":"LastName",
        "birthday":null
    }
}

How can I parse JSONObject without knowing the name "2" just moving to the next array?

3
  • 2
    what json parser are you using? Commented Jul 1, 2015 at 10:09
  • Depends on your parser. Commented Jul 1, 2015 at 10:09
  • 2
    Yes you can. First parse the string into JSONObject then key the keySet() & the iterate over the keySet() to get individual objects. Commented Jul 1, 2015 at 10:10

1 Answer 1

7
String json="" // place your json format here in double Quotes with proper escapes .......
jObject = new JSONObject(json.trim());
Iterator<?> keys = jObject.keys();

while( keys.hasNext() ) {
    String key = (String)keys.next();
    if ( jObject.get(key) instanceof JSONObject ) {
         // do what ever you want with the JSONObject.....
    }
}
Sign up to request clarification or add additional context in comments.

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.