1

I have the following hashMap:

{"success":true,
 "message":"Profile retrieved successfully",
 "data":{
     "address":[{
        "objectId":"6ItcdQGBFu",
        "street":"6782 NW 102nd St",
        "aptSuite":"782",
        "state":"FL",
        "zipCode":"33762",
        "city":"Brickell",
        "type":"Home"},{
        "objectId":"yyRA9M2gk1", 
        "street":"7828 NW Boston Rb",
        "state":"Massachusetts",
        "zipCode":"33178",
        "city":"Boston",
        "type":"Office"}
     ]
    }
}

If I want to access the "message", I would write the following code:

    object.get("message");

Or if I wanna print it:

    object.get("message").toString();

I would like to know how can I iterate and access through the "address" objects.

3
  • 4
    We have no idea, because we don't know the type of the object at the key "data". Commented Jan 6, 2017 at 21:11
  • 2
    That is not a hashmap. That is a JSON string. Commented Jan 6, 2017 at 21:14
  • I think you should use JSON parsers for this kind of object, that I will help you on this, please check Jackson lib Commented Jan 6, 2017 at 21:59

1 Answer 1

1

Assuming this is a bunch of nested HashMaps (and "address" points to a list with one item, according to your example), you could access a value in the message through

object.get("data").get("address").get(0).get("street")

and you could iterate through all the fields by

HashMap<String, String> addressObj = object.get("data").get("address").get(0);
for (Map.Entry<String, String> entry : addressObj.keySet()) {
    String key = entrey.getKey();
    String value = entry.getValue();
    // your code here
}
Sign up to request clarification or add additional context in comments.

1 Comment

sounds reasonable, if that's the case, the variable "object" should be declared like this? Map<String, Map<String, Map<String, ArrayList>>> object

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.