使用Jackson JSON Parser:复杂的JSON? [英] Using Jackson JSON Parser: Complex JSON?

查看:137
本文介绍了使用Jackson JSON Parser:复杂的JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个复杂的JSON,我试图使用Jackson JSON解析。我对如何进入latLng对象以拉出lat,lng值感到有点困惑。这是JSON的一部分:

I have a complex JSON that I am trying to parse using Jackson JSON. I am a little confused about how to get into the latLng object to pull out the lat,lng values. This is part of the JSON:

{
    "results": [
        {
            "locations": [
                {
                    "latLng": {
                        "lng": -76.85165,
                        "lat": 39.25108
                    },
                    "adminArea4": "Howard County",
                    "adminArea5Type": "City",
                    "adminArea4Type": "County",

这就是我迄今为止用Java来解决的问题:

This is what I have so far in Java to pull it out:

public class parkJSON
{
    public latLng _latLng;

    public static class latLng
    {
        private String _lat, _lng;
        public String getLat() { return _lat; }
        public String getLon() { return _lng; }
    } 
}

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
parkJSON geo = mapper.readValue(parse, parkJSON.class);

System.out.println(mapper.writeValueAsString(geo));  
String lat = geo._latLng.getLat();
String lon = geo._latLng.getLon();
output = lat + "," + lon;
System.out.println("Found Coordinates: " + output);

已解决这是我通过树模型将来解决问题的方法参考:

RESOLVED This is how I solved the issue by using Tree Model for future reference:

            ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
            mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);                 
            JsonNode rootNode = mapper.readTree(parse);
            JsonNode firstResult = rootNode.get("results").get(0);
            JsonNode location = firstResult.get("locations").get(0);
            JsonNode latLng = location.get("latLng");
            String lat = latLng.get("lat").asText();
            String lng = latLng.get("lng").asText();
            output = lat + "," + lng;
            System.out.println("Found Coordinates: " + output);


推荐答案

如果你真的对这个输入感兴趣的话结构是lat和lng完全映射可能是Jackson提供的不同方法中最不适应的,因为它迫使你编写类来表示数据中的不同层。

If all you're really interested in in this input structure are lat and lng full mapping is probably the least adapted of the different approaches offered by Jackson, as it forces you to write classes to represent the different layers in your data.

Jackson提供了两种替代方案,允许您在不必定义这些类的情况下提取这些字段:

There are two alternatives offered by Jackson that will allow you to extract these fields without having to define these classes:


  1. 树模型提供了许多导航方法来遍历树并提取您感兴趣的数据。

  2. 简单数据绑定将JSON文档映射到地图或然后可以使用这些集合提供的方法导航列表。

  1. The tree model offers a number of navigation methods to traverse the tree and extract the data you're interested in.
  2. Simple data binding maps the JSON document onto a Map or a List which can then be navigated with the methods offered by these collections.

Jackson纪录片tation包含两种技术的示例,在程序中应用它们应该不会太难,使用调试器调查解析器创建的数据结构,以查看文档的映射方式。

The Jackson documentation contains examples for both techniques, applying them in your program should not be too hard, use your debugger to investigate the data structures created by the parser to see how the document got mapped.

这篇关于使用Jackson JSON Parser:复杂的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆