在 HashMap 中获取数组值中的值

Get Value in Array Value in HashMap

 "results": 
[
        {
            "name": "xxx",
            "Data": {
                "id": 23
            }
        }
]

我有一个 json 像这样返回,我用 HashMap 遇到它,但我可以访问数据数组。如何获取数据数组中的 ID?

List<HashMap<String, Object>> personList = getCurrentResponse().jsonPath().getList("results");
   for (HashMap<String, Object> person : personList ) {
      if (person .get("name").equals(name)) {
          int id= (int) (person .get("Data.id"));
          return id;

我从上面的代码示例中得到 NullException

您应该首先将数据作为一个 HashMap 获取,然后从中获取 ID:

HashMap<String, Object> data = (HashMap<String, Object>) person .get("Data");
int id= (int) (data.get("id"));

您一次只能检索一个级别,您可以调用.get()两次

for (HashMap<String, Object> person : personList ) {
    if (person .get("name").equals(name)) {
        return person.get("Data").get("id");
    }
}