如何使用`com.fasterxml.jackson.core.JsonParser`确定JSON数组的大小

huangapple go评论81阅读模式
英文:

How to determine the size of json array using `com.fasterxml.jackson.core.JsonParser`

问题

以下是翻译好的内容:

我正在使用一个内部库,该库公开了一个 API,用于解析 JSON 值。这个 API 提供了com.fasterxml.jackson.core.JsonParser,可以利用它,它已经使用适当的 JSON 流进行了实例化,以便进行读取。现在,如果假设此 JsonParser 实例正在解析的 JSON 流是一个数组对象,我该如何确定数组的大小。

我可以通过使用此 JsonParser 实例实例化一个 ObjectMapper,并创建一个带有列表的容器 Java 类(表示数组),最后确定大小。我想知道是否有可能仅使用 JsonParser 就能够做到这一点,而不使用 ObjectMapper,因此也不需要创建虚拟的容器 Java 类。

class ResultContainer {
 private List<Object> result;
 
 public List<Object> getResult() {
   return result;
 }
 
 public void setResult(List<Object> result) {
   this.result = result;
 }
}
----
void parseEntity(JsonParser parser) {
  ObjectMapper om = new ObjectMapper();
  ResultContainer rc = om.readValue(parser, ResultContainer.class);
  System.out.println(rc.getResult().size());
}

在上面的示例中,是否有一种方法可以仅使用 JsonParser 获取数组的大小,而不使用 ObjectMapper,因此不需要创建虚拟的容器 Java 类。

英文:

I am using an internal library which exposes an api, to parse a json value. This api provides com.fasterxml.jackson.core.JsonParser to make use of, which is already instantiated with the appropriate json stream to be read. Now, how do I determine the size of an array, assuming the json stream this JsonParser instance is parsing is an array object.

I can find a way by instantiating an ObjectMapper using this JsonParser instance, and create a container java class with a list in it (representing the array) and finally determine the size. I was wondering if its possible to do it just with JsonParser without using ObjectMapper.

class ResultContainer {
 private List&lt;Object&gt; result;
 
 public List&lt;Object&gt; getResult() {
   return result;
 }
 
 public void setResult(List&lt;Object&gt; result) {
   this.result = result;
 }
}
----
void parseEntity(JsonParser parser) {
  ObjectMapper om = new ObjectMapper();
  ResultContainer rc = om.readValue(parser, ResultContainer.class);
  System.out.println(rc.getResult().size());
}

In the above example, is there a way I can get the size of the array with just the JsonParser, without using ObjectMapper and therefore without creating a dummy container java class.

答案1

得分: 2

当然,您可以将 JSON 读入 JsonNode:https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/ObjectMapper.html

JsonNode root = mapper.readTree(...);

然后分析这个 JsonNode。可以查看示例:https://www.baeldung.com/jackson-json-node-tree-model#2-handling-different-node-types

英文:

Sure, you can read JSON into JsonNode: https://fasterxml.github.io/jackson-databind/javadoc/2.8/com/fasterxml/jackson/databind/ObjectMapper.html

JsonNode root = mapper.readTree(...);

and then analyze this JsonNode. Take a look at examples: https://www.baeldung.com/jackson-json-node-tree-model#2-handling-different-node-types

huangapple
  • 本文由 发表于 2020年5月4日 01:00:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/61578394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定