简体   繁体   English

如何将json对象列表转换为包含每个对象中某些字段的字符串pojo列表?

[英]How to convert list of json objects to pojo list of strings that contains some field from each object?

I have a JSON document that describe list of objects, it looks something like this: 我有一个描述对象列表的JSON文档,它看起来像这样:

[
   {
     "txId": "ffff",
     "sender" : "0xwwwwwww",
     "recepient" : "0xeferfef"
   },
   {
     "txId": "ffff",
     "sender" : "0xwwwwwww",
     "recepient" : "0xeferfef"
   }
   ...

   ...
]

How can I get List<String> that contains txId values from each object using only Jackson API (without converting this JSON to a list of pojo-objects then proceed this list by foreach and create new list of strings)? 如何仅使用Jackson API从每个对象中获取包含txId值的List<String> (无需将此JSON转换为pojo对象列表,然后通过foreach处理此列表并创建新的字符串列表)?

You can always read a JSON document as JsonNode object with Jackson API (no need of creating POJO). 您始终可以使用Jackson API将JSON文档作为JsonNode对象读取(无需创建POJO)。 Next, there are several ways of reading and manipulating the data represented as JsonNode object. 接下来,有几种读取和处理以JsonNode对象表示的数据的方式。 One of the most convenient ways available from Java 8+ is to create a java.util.Stream<JsonNode> and collect the final list as a result of a mapping from JsonNode to String , where String is represents a value of node.txId field. Java 8+提供的最方便的方法之一是创建一个java.util.Stream<JsonNode>并收集从JsonNodeString的映射结果的最终列表,其中String表示node.txId字段的值。

You can create java.util.Stream<JsonNode> with : 您可以使用以下命令创建java.util.Stream<JsonNode>

java.util.stream.StreamSupport.stream(jsonNode.spliterator(), false)

and then you can call map(node -> node.get("txId").textValue() and finally call collect() to terminate the stream and get your expected result. 然后您可以调用map(node -> node.get("txId").textValue() ,最后调用collect()终止流并获得预期的结果。

Consider following code as an example (requires Java 8+): 考虑以下代码作为示例(需要Java 8+):

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

final class JacksonReadExample {

    public static void main(String[] args) throws IOException {

        final String json = " [\n" +
                "   {\n" +
                "     \"txId\": \"ffff-001\",\n" +
                "     \"sender\" : \"0xwwwwwww\",\n" +
                "     \"recepient\" : \"0xeferfef\"\n" +
                "   },\n" +
                "   {\n" +
                "     \"txId\": \"ffff-002\",\n" +
                "     \"sender\" : \"0xwwwwwww\",\n" +
                "     \"recepient\" : \"0xeferfef\"\n" +
                "   }\n" +
                "]";

        final ObjectMapper mapper = new ObjectMapper();

        final JsonNode jsonNode = mapper.readTree(json);

        final List<String> ids = StreamSupport.stream(jsonNode.spliterator(), false)
                .map(node -> node.get("txId").textValue())
                .collect(Collectors.toList());

        System.out.println(ids);
    }
}

Output: 输出:

[ffff-001, ffff-002]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM