使用Java和Jackson将Json序列化为无需架构的通用结构 [英] Serialize Json into generic structure without schema using Java and Jackson

查看:86
本文介绍了使用Java和Jackson将Json序列化为无需架构的通用结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要序列化JSON而不附加到结果对象的特定模式,例如,某些通用的set / map / hashmap。

I have a need to serialize a JSON without being attached to particular schema for resulting object, e.g., to some generic set/map/hashmap.

作为输入,我有一个带有JSON的字符串。我不知道该JSON的架构。

As input, I have a string with a JSON. I do not know schema for that JSON.

作为输出,我想要一个具有键值序列化的Java对象,如Hashmap或类似物输入。

As output, I want a Java Object such as Hashmap or similar that has key-value serialization of input.

请注意输入JSON包含基本字段和数组/列表。

Note that input JSON has both basic fields and Array/List inside it.

我必须使用Java和Jackson(或其他一些库)。我怎么可能这样做?

I have to use Java and Jackson (or some other library). How I possibly can do that?

推荐答案

杰克逊数据绑定能够使用String键和对象将任何json输入读入Map值(也可以是地图或集合)。您只需告诉映射器您想将json读入地图。您可以通过为映射器提供适当的类型引用来实现:

Jackson data binding is able to read any json input into a Map with String key and Object value (that can be also a map or collection). You just tell the mapper that you would like to read the json into a map. You do that by giving the mapper the appropriate type reference:

import java.util.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Test
{
    public static void main(String[] args)
    {
        try {
            String json = "{ "
                    + "\"string-property\": \"string-value\", "
                    + "\"int-property\": 1, "
                    + "\"bool-property\": true, "
                    + "\"collection-property\": [\"a\", \"b\", \"c\"], "
                    + "\"map-property\": {\"inner-property\": \"inner-value\"} "
                    + "}";

            ObjectMapper mapper = new ObjectMapper();
            Map<String, Object> map = new HashMap<>();
            // convert JSON string to Map
            map = mapper.readValue(json, new TypeReference<Map<String, Object>>(){});

            System.out.println("input: " + json);
            System.out.println("output:");
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                System.out.println("key: " + entry.getKey());
                System.out.println("value type: " + entry.getValue().getClass());
                System.out.println("value: " + entry.getValue().toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}   

输出:

input: { "string-property": "string-value", "int-property": 1, "bool-property": true, "collection-property": ["a", "b", "c"], "map-property": {"inner-property": "inner-value"} }
output:
key: string-property
value type: class java.lang.String
value: string-value
key: int-property
value type: class java.lang.Integer
value: 1
key: bool-property
value type: class java.lang.Boolean
value: true
key: collection-property
value type: class java.util.ArrayList
value: [a, b, c]
key: map-property
value type: class java.util.LinkedHashMap
value: {inner-property=inner-value}

这篇关于使用Java和Jackson将Json序列化为无需架构的通用结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆