简体   繁体   English

带有数组的嵌套HashMap,JAVA

[英]Nested HashMap with Array, JAVA

I have the following hashMap: 我有以下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. 我想知道如何迭代和访问“地址”对象。

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 假设这是一堆嵌套的HashMap(根据您的示例,“地址”指向包含一个项目的列表),则可以通过以下方式访问消息中的值

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
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM