How to convert a JSONobject into an object map?

时间:2021-10-06 21:02:08

I’ve been trying to create a dynamic text-grabbing system to be able to translate future programs into different languages more easily. I’m fairly new to Java, so I’m not well versed on the data types that there are, but would it be possible to take a JSON object (using simple.json) and converting it into something that I could reference easily and concisely?

我一直在尝试创建一个动态的文本抓取系统,以便能够更轻松地将未来的程序翻译成不同的语言。我对Java很新,所以我不太熟悉有的数据类型,但是可以使用JSON对象(使用simple.json)并将其转换为我可以轻松引用的内容。简洁?

For example given a JSON string:

例如,给定一个JSON字符串:

{
    "name": "John Doe",
    "country": "US",
    "age": 25,
    "family": {
        "immediate": {
            "spouse": "Johnette Doe",
            "children": [
                {
                    "name": "Jimbles Doe",
                    "age": "213"
                }
            ]
        }
    }
}

How could I set my file up so that I could reference it like so:

我怎么能设置我的文件,以便我可以像这样引用它:

JohnClass.family.immediate.children[0].name

and get a value in return?

并得到一个值作为回报?

1 个解决方案

#1


1  

Get Jackson (either use the maven dependency or download the jar): http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.5.0

获取Jackson(使用maven依赖或下载jar):http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.5.0

Untested code below, and to be honest a effortless punt, just to help you sort it quickly.

下面未经测试的代码,老实说是一个毫不费力的平底船,只是为了帮助您快速排序。

Instantiate JSONMapper (ideally in a constructor):

实例化JSONMapper(理想情况下在构造函数中):

ObjectMapper mapper = new ObjectMapper();

Convert to Hashmap (you could also convert to any other object):

转换为Hashmap(您还可以转换为任何其他对象):

Map map = mapper.readValue("JSON_STRING_HERE", new TypeReference<HashMap>(){});

Access like this:

这样访问:

map.get("family").get("immediate").get("children").get(0).get("name");

You could also just use JSONObject which also implements the Map interface, but if you get used to do it this way you'll know how to do it for other objects.

您也可以使用JSONObject同时实现Map接口,但如果您习惯这样做,您将知道如何为其他对象执行此操作。

#1


1  

Get Jackson (either use the maven dependency or download the jar): http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.5.0

获取Jackson(使用maven依赖或下载jar):http://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind/2.5.0

Untested code below, and to be honest a effortless punt, just to help you sort it quickly.

下面未经测试的代码,老实说是一个毫不费力的平底船,只是为了帮助您快速排序。

Instantiate JSONMapper (ideally in a constructor):

实例化JSONMapper(理想情况下在构造函数中):

ObjectMapper mapper = new ObjectMapper();

Convert to Hashmap (you could also convert to any other object):

转换为Hashmap(您还可以转换为任何其他对象):

Map map = mapper.readValue("JSON_STRING_HERE", new TypeReference<HashMap>(){});

Access like this:

这样访问:

map.get("family").get("immediate").get("children").get(0).get("name");

You could also just use JSONObject which also implements the Map interface, but if you get used to do it this way you'll know how to do it for other objects.

您也可以使用JSONObject同时实现Map接口,但如果您习惯这样做,您将知道如何为其他对象执行此操作。