简体   繁体   English

使用 Jackson 读取最外面的 JSON object 但不读取内部的?

[英]Read the outer-most JSON object but not the inner ones using Jackson?

This is similar to this question but it's a little different.这与问题相似,但略有不同。

Let's say I have a json document defined like this:假设我有一个 json 文档定义如下:

[
    { "type" : "Type1", 
      "key1" : "value1" },
    { "type" : "Type2", 
      "key2" : "value2" }
]

I want to read this json document into a list of strings ( List<String> ).我想将此 json 文档读入字符串列表( List<String> )。 I only want to read the outer-most list into the Java List , the json objects inside the list should be left as-is inside the list.我只想将最外面的列表读入 Java List中,列表中的 json 对象应按原样保留在列表中。 The result should be equivalent to this (I ignore newlines etc):结果应该与此等效(我忽略换行符等):

var myList = List.of("{\"type\": \"Type1\", \"key1\": \"value1\"}, {\"type\": \"Type2\", \"key2\": \"value2\"}")

Note that I don't want to create any DTO to hold some intermediate representation.请注意,我不想创建任何 DTO 来保存一些中间表示。 I just want everything below the "list" to be represented "as-is".我只想“按原样”表示“列表”下方的所有内容。

How can I achieve this?我怎样才能做到这一点?

I'm using Jackson 2.12.1.我正在使用 Jackson 2.12.1。

If you don't want to hold intermediate representation in a DTO, then one way in which the required deserialization can be achieved is:如果您不想在 DTO 中保留中间表示,那么可以实现所需反序列化的一种方法是:

// Create a ObjectMapper (of type com.fasterxml.jackson.databind.ObjectMapper)
ObjectMapper mapper = new ObjectMapper();
// Read the json string into a List. This will be deserialized as a collection of LinkedhashMap
 List<LinkedHashMap> list = mapper.readValue(getInputString(), List.class);
//Iterate over the deserialized collection and create a JSONObject from every LinkedHashMap
 List<String> result = list.stream()
                           .map(map -> new JSONObject(map).toString())
                           .collect(Collectors.toList());

This will produce:这将产生:

[{"key1":"value1","type":"Type1"}, {"key2":"value2","type":"Type2"}]

Downside of this approach is, it takes a hit on performance.这种方法的缺点是,它会影响性能。

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

相关问题 使用jackson打开内部json对象 - unwrap inner json object using jackson 从JSON文件中的JSON数组读取对象数组(使用Jackson) - Read Object Array From JSON Array in a JSON file (using Jackson) 使用Jackson读取Json文件以获取以下Json结构并获取对象 - Read Json file using Jackson for the following Json structure and Obtain the Object 将内部 JSON object 提取为 Jackson 中的字符串 - Extract the inner JSON object as String in Jackson 使用杰克逊(ObjectMapper)如何将对象序列化为json并忽略除我注释为@JsonProperty的字段以外的所有其他字段? - Using Jackson (ObjectMapper) how serialize object to json and ignore all fields except the ones I annotate as @JsonProperty? 当内部json对象有条件为空时,使用Jackson将JSON对象映射到Java - Mapping a JSON object to Java using Jackson when an inner json object is conditionally empty Json:如何使用Java从单个外部Json对象提取内部Json对象 - Json: How to extract inner Json objects from a single outer Json object using java 如何使用 Jackson 从 Json 文件中读取特定的 object - How to read a specific object from a Json file using Jackson 使用杰克逊在JSON中插入和删除内部节点? - Using jackson to insert and remove inner nodes in a JSON? 使用JACKSON读取JSON文件 - Read a JSON file using JACKSON
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM