简体   繁体   English

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

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

I am using an internal library which exposes an api, to parse a json value.我正在使用公开 api 的内部库来解析 json 值。 This api provides com.fasterxml.jackson.core.JsonParser to make use of, which is already instantiated with the appropriate json stream to be read. 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.现在,我如何确定数组的大小,假设 json stream 这个 JsonParser 实例正在解析的是一个数组 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.我可以通过使用此 JsonParser 实例实例化 ObjectMapper 找到一种方法,并创建一个容器 java class ,其中包含一个列表(表示数组),最后确定大小。 I was wondering if its possible to do it just with JsonParser without using ObjectMapper.我想知道是否可以只使用 JsonParser 而不使用 ObjectMapper。

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());
}

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.在上面的示例中,有没有一种方法可以仅使用 JsonParser 获取数组的大小,而无需使用 ObjectMapper,因此无需创建虚拟容器 java class。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何克隆`com.fasterxml.jackson.core.JsonParser` - How to clone `com.fasterxml.jackson.core.JsonParser` 使用 com.fasterxml.jackson.core.JsonParser 作为 Jersey 客户端序列化器/反序列化器? - Using com.fasterxml.jackson.core.JsonParser as Jersey client serializer/deserializer? com.fasterxml.jackson.core.JsonParseException:无法识别的令牌'Hello':期待(JSON字符串,数字,数组,) - com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Hello': was expecting (JSON String, Number, Array,) 如何使用jackson(com.fasterxml.jackson)消耗相同密钥的不一致json结构? - How to consume the inconsistent json structure for the same key using jackson (com.fasterxml.jackson)? com.fasterxml.jackson.core.JsonParseException是否为* .json.swp? - com.fasterxml.jackson.core.JsonParseException for *.json.swp? NoClassDefFoundError com/fasterxml/jackson/core/TreeNode — json 模式验证 - NoClassDefFoundError com/fasterxml/jackson/core/TreeNode — json schema validation Jackson NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory - Jackson NoClassDefFoundError: com/fasterxml/jackson/core/JsonFactory 使用com.fasterxml.jackson.databind.JsonNode从Java中的json数组解析json对象; - parse json object from json array in java using com.fasterxml.jackson.databind.JsonNode; How to convert CSV to JSON with value in double quotes using com.fasterxml.jackson.dataformat.csv.CsvMapper in Java? - How to convert CSV to JSON with value in double quotes using com.fasterxml.jackson.dataformat.csv.CsvMapper in Java? 从Servlet调用Jackson解析器时获取ClassNotFoundException:com.fasterxml.jackson.core.JsonProcessingException - Getting ClassNotFoundException: com.fasterxml.jackson.core.JsonProcessingException when calling Jackson JSON parser from a servlet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM