有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Java中使用JsonPath解析JSON中的数组

我使用jayway JsonPath库使用给定的路径解析JSON。如果我给出正确的路径,我就能得到字符串值。但是,如果路径是数组,我想在给出路径时得到贴图列表。例如,我有一个JSON,如下所示:

{
    "employees": {
        "company": "Google",
        "people": [{
            "name": "John",
            "age": 25,
            "location": "zurich"
        },
        {
            "name": "Peter",
            "age": 27,
            "location": "Lucerene"
        }]
    }
}

下面是我用来解析json的代码

如果我给出路径$.employees.people,我将得到字符串,但我需要列出映射。下面是我使用jsonpath解析Json的代码

DocumentContext documentContext = JsonPath.parse(jsonStr);
JsonPath jsonPath = JsonPath.compile("$.employees.people");
List<Maps<String,String>> jsonList = documentContext.read(jsonPath) //But this is returning String

有人建议采取适当的方法来达到我的期望吗


共 (1) 个答案

  1. # 1 楼答案

    试着用

    DocumentContext documentContext = JsonPath.parse(jsonStr);
    JsonPath jsonPath = JsonPath.compile("$.employees.people[?]");
    List<Maps<String,String>> jsonList = documentContext.read(jsonPath);
    

    如果你需要更多细节readmore...