Reading list Switch to dark mode

    Iterate through JSONObject in java (android)

    Updated 16 July 2021

    Assuming your JSON is in a string form, you can iterate over the attribute-value pairs as follows:

    Below String jsonstring is the string of json.

    String jsonstring = "{ "child": { "something": "value", "something2": "value" } }";
    JSONObject resobj = new JSONObject(jsonstring);
    Iterator<?> keys = resobj.keys();
    while(keys.hasNext() ) {
        String key = (String)keys.next();
        if ( resobj.get(key) instanceof JSONObject ) {
            JSONObject xx = new JSONObject(resobj.get(key).toString());
            Log.d("res1",xx.getString("something"));
            Log.d("res2",xx.getString("something2"));
        }
    }

    Searching for an experienced
    Magento Company ?
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home