使用 Java 读取 JSON 中嵌套键的值 (Jackson)

问题描述 投票:0回答:4

我是一名具有 Python 背景的新 Java 程序员。我有以 JSON 形式收集/返回的天气数据,其中包含嵌套键,但我不明白在这种情况下如何提取这些值。我确信这个问题以前曾被问过,但我发誓我已经用谷歌搜索了很多,但我似乎找不到答案。现在我正在使用 json-simple,但我尝试切换到 Jackson,但仍然不知道如何做到这一点。由于 Jackson/Gson 似乎是最常用的库,我很想看到使用这些库之一的示例。下面是数据示例,后面是我迄今为止编写的代码。

{
    "response": {
        "features": {
            "history": 1
        }
     },
    "history": {
        "date": {
            "pretty": "April 13, 2010",
            "year": "2010",
            "mon": "04",
            "mday": "13",
            "hour": "12",
            "min": "00",
            "tzname": "America/Los_Angeles"
        },
        ...
    }
}

主要功能

public class Tester {

    public static void main(String args[]) throws MalformedURLException, IOException, ParseException {
        WundergroundAPI wu =  new WundergroundAPI("*******60fedd095");

        JSONObject json = wu.historical("San_Francisco", "CA", "20100413");

        System.out.println(json.toString());
        System.out.println();
        //This only returns 1 level. Further .get() calls throw an exception
        System.out.println(json.get("history"));
    }
}

“历史”函数调用另一个返回 JSONObject 的函数

public static JSONObject readJsonFromUrl(URL url) throws MalformedURLException, IOException, ParseException {

    InputStream inputStream = url.openStream();

    try {
        JSONParser parser = new JSONParser();
        BufferedReader buffReader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));

        String jsonText = readAll(buffReader);
        JSONObject json = (JSONObject) parser.parse(jsonText);
        return json;
    } finally {
        inputStream.close();
    }
}
java json jackson gson
4个回答
119
投票

使用 Jackson 的树模型 (

JsonNode
),您可以同时拥有“文字”访问器方法 ('get')(它针对缺失值返回
null
)和“安全”访问器 ('path')(允许您遍历) “丢失”的节点。因此,例如:

JsonNode root = mapper.readTree(inputSource);
int h = root.path("response").path("history").getValueAsInt();

这将返回给定路径的值,或者,如果路径丢失,则返回 0(默认值)

但更方便的是,你可以直接使用 JSON 指针表达式:

int h = root.at("/response/history").getValueAsInt();

还有其他方法,通常将结构建模为普通旧 Java 对象 (POJO) 更方便。 您的内容可能类似于:

public class Wrapper {
  public Response response;
} 
public class Response {
  public Map<String,Integer> features; // or maybe Map<String,Object>
  public List<HistoryItem> history;
}
public class HistoryItem {
  public MyDate date; // or just Map<String,String>
  // ... and so forth
}

如果是这样,您将像任何 Java 对象一样遍历结果对象。


8
投票

使用Jsonpath

Integer h = JsonPath.parse(json).read("$.response.repository.history", Integer.class);

1
投票

查看 Jackson 的 ObjectMapper。您可以创建一个类来对 JSON 进行建模,然后使用 ObjectMapper 的 readValue 方法将 JSON 字符串“反序列化”为模型类的实例。反之亦然。


0
投票

尝试 jpath API。它相当于 JSON 数据的 xpath。您可以通过提供 jpath 来读取数据,该 jpath 将遍历 JSON 数据并返回请求的值。

这个 Java 类是实现,并且有关于如何调用 API 的示例代码。

https://github.com/satyapaul/jpath/blob/master/JSONDataReader.java

自述文件 -

https://github.com/satyapaul/jpath/blob/master/README.md

示例:

JSON 数据:

{
    "data": [{
        "id": "13652355666_10154605514815667",
        "uid": "442637379090660",
        "userName": "fanffair",
        "userFullName": "fanffair",
        "userAction": "recommends",
        "pageid": "usatoday",
        "fanPageName": "USA TODAY",
        "description": "A missing Indonesian man was found inside a massive python on the island of Sulawesi, according to local authorities and news reports. ",
        "catid": "NewsAndMedia",
        "type": "link",
        "name": "Indonesian man swallowed whole by python",
        "picture": "https:\/\/external.xx.fbcdn.net\/safe_image.php?d=AQBQf3loH5-XP6hH&w=130&h=130&url=https%3A%2F%2Fwww.gannett-cdn.com%2F-mm-%2F1bb682d12cfc4d1c1423ac6202f4a4e2205298e7%2Fc%3D0-5-1821-1034%26r%3Dx633%26c%3D1200x630%2Flocal%2F-%2Fmedia%2F2017%2F03%2F29%2FUSATODAY%2FUSATODAY%2F636263764866290525-Screen-Shot-2017-03-29-at-9.27.47-AM.jpg&cfs=1&_nc_hash=AQDssV84Gt83dH2A",
        "full_picture": "https:\/\/external.xx.fbcdn.net\/safe_image.php?d=AQBQf3loH5-XP6hH&w=130&h=130&url=https%3A%2F%2Fwww.gannett-cdn.com%2F-mm-%2F1bb682d12cfc4d1c1423ac6202f4a4e2205298e7%2Fc%3D0-5-1821-1034%26r%3Dx633%26c%3D1200x630%2Flocal%2F-%2Fmedia%2F2017%2F03%2F29%2FUSATODAY%2FUSATODAY%2F636263764866290525-Screen-Shot-2017-03-29-at-9.27.47-AM.jpg&cfs=1&_nc_hash=AQDssV84Gt83dH2A",
        "message": "Akbar Salubiro was reported missing after he failed to return from harvesting palm oil.",
        "link": "http:\/\/www.usatoday.com\/story\/news\/nation-now\/2017\/03\/29\/missing-indonesian-man-swallowed-whole-reticulated-python\/99771300\/",
        "source": "",
        "likes": {
            "summary": {
                "total_count": "500"
            }
        },
        "comments": {
            "summary": {
                "total_count": "61"
            }
        },
        "shares": {
            "count": "4"
        }
    }]

}

代码片段:

String jPath = "/data[Array][1]/likes[Object]/summary[Object]/total_count[String]";

String value = JSONDataReader.getStringValue(jPath, jsonData);
© www.soinside.com 2019 - 2024. All rights reserved.