简体   繁体   English

将JsonObject从Google转换为JsonNode再转换为Jackson,然后将多个JsonNode作为数组传递给Web服务

[英]Convert JsonObject from google to JsonNode to Jackson and then pass multiple JsonNode as an array to a web service

I have an application which has a list of JsonObject and which uses google GSON library. 我有一个具有JsonObject列表并使用google GSON库的应用程序。 I need to send this list to my another service, which is using the Jackson library for JSON serialization and deserialization, this service extracts the each JsonObject and does some processing. 我需要将此列表发送到另一个服务,该服务使用Jackson库进行JSON序列化和反序列化,此服务提取每个JsonObject并进行一些处理。

I have an experience of handling the JSON, coming to my web-service using the JsonNode of jackson in my Jersey resource. 我有处理JSON的经验,使用Jersey资源中的jackson的JsonNode进入我的Web服务。 But in this case it would be an array of JsonNode. 但是在这种情况下,它将是JsonNode的数组。

[
    {
        "id" : 1,
        "firstName": "hello",
        "lastName": "world"
    },
    {
        "id" : 2,
        "firstName": "first",
        "lastName": "last"
    }
]

So my question is using which datatype of Jackson , I should be able to handle the array of JsonNode ? 所以我的问题是使用Jackson哪种数据类型,我应该能够处理JsonNode的数组?

found the solution, we can use the same JsonNode to handle the array of JSON, And then can use the size method of JsonNode to determine how many elements are there in JsonArray which is of type JsonNode . 找到解决方案后,我们可以使用相同的JsonNode处理JSON数组,然后可以使用JsonNode的size方法确定JsonArray中类型为JsonNode元素JsonNode

Code sample: 代码示例:

public void test(JsonNode jsonNode) throws IOException {
        int size = jsonNode.size();
        HashMap<String, String> map = new HashMap<>();
        while (size > 0) {
            JsonNode message = jsonNode.get(--size);
            map.put(message.get("id").toString(), message.toString());
        }

}

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

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