java 使用流/对象方法将 JSON 解析为 Jackson

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13466166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:51:57  来源:igfitidea点击:

Parsing JSON into Hymanson using a stream/object approach

javajsonparsingHymanson

提问by Anonymous Person

I have a JSON file which can have multiple types.

我有一个可以有多种类型的 JSON 文件。

For example:

例如:

{
    "dog": {
        "owner" : "John Smith",
        "name" : "Rex",
        "toys" : {
            "chewtoy" : "5",
            "bone" : "1"
        }
    },
    "person": {
        "name" : "John Doe",
        "address" : "23 Somewhere Lane"
    }
    // Further examples of dogs and people, and a few other types.
}

I want to parse these into objects. ie. I want to create a Dog object with owner/name/toys attributes, and person with name/address attributes, and use Hymanson to read through and create objects out of them.

我想将这些解析为对象。IE。我想创建一个具有所有者/姓名/玩具属性的 Dog 对象和具有姓名/地址属性的人,并使用 Hymanson 阅读并从中创建对象。

The ordering matters - I need to know that Rex came before John Doe, for example. I would prefer to do with a stream like approach (ie. read and parse Rex into the Dog object, do something with it, then discard it, then move onto to John Doe). So I need a stream based approach.

排序很重要——例如,我需要知道 Rex 出现在 John Doe 之前。我更喜欢使用类似流的方法(即,读取 Rex 并将其解析为 Dog 对象,对其进行处理,然后丢弃它,然后转到 John Doe)。所以我需要一种基于流的方法。

I can't figure out how to use both the stream reading API (to go through in order) and the ObjectMapper interface (in order to create Java objects out of JSON) to accomplish this.

我不知道如何使用流读取 API(按顺序执行)和 ObjectMapper 接口(以便从 JSON 创建 Java 对象)来完成此操作。

回答by Christian Trimble

To do this, you need to use an object mapper with your factory

为此,您需要在工厂中使用对象映射器

import org.codehaus.Hymanson.JsonFactory;
import org.codehaus.Hymanson.JsonParser;
import org.codehaus.Hymanson.JsonProcessingException;
import org.codehaus.Hymanson.map.ObjectMapper;
...
private static ObjectMapper mapper = new ObjectMapper();
private static JsonFactory factory = mapper.getJsonFactory();

Then create a parser for the input.

然后为输入创建一个解析器。

JsonParser parser = factory.createJsonParser(in);

Now you can mix calls to parser.nextToken()and calls to parser.readValueAs(Class c). Here is an example that gets the classes from a map:

现在您可以混合调用parser.nextToken()和调用parser.readValueAs(Class c)。这是从地图中获取类的示例:

Map<String, Class<?>> classMap = new HashMap<String, Class<?>>();
classMap.put("dog", Dog.class);
classMap.put("person", Person.class);

InputStream in = null;
JsonParser parser = null;
List<Object> results = new ArrayList<Object>();
try {
  in = this.getClass().getResourceAsStream("input.json");
  parser = factory.createJsonParser(in);
  parser.nextToken();// JsonToken.START_OBJECT
  JsonToken token = null;
  while( (token = parser.nextToken()) == JsonToken.FIELD_NAME ) {
    String name = parser.getText();
    parser.nextToken(); // JsonToken.START_OBJECT
    results.add(parser.readValueAs(classMap.get(name)));
  }
  // ASSERT: token = JsonToken.END_OBJECT
}
finally {
  IOUtils.closeQuietly(in);
  try {
    parser.close();
  }
  catch( Exception e ) {}
}