Java将JSON转换为哈希映射

Java将JSON转换为哈希映射

本文介绍了使用Jackson-Java将JSON转换为哈希映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此之前被标记为重复,请阅读问题(我确实看过类似的问题)。谢谢。

Before this is marked as a duplicate please read the question (I did look at similar ones). Thank you.

为简单起见,假设我有这样的JSON:

For simplicity, assume I have JSON like this:

{
   "clients" : [
   {
      "name" : "client 1",
      "id" : 1
   },
   {
      "name" : "client 2",
      "id" : 2
   }
],
   "other" : {
      "something" : ""
   }

   ...
}

所以我想创建一个只有客户端及其字段的哈希映射。基本问题是如何使用Jackson方法为客户端的单个JSON数组执行此操作?我试过在网上看,但我看到的所有例子都不使用Jackson,或者仅用于单个JSON对象:

So I want to create a hash map of only the clients and their fields. The basic question is how would I go about doing this using Jackson methods for a single JSON array like clients? I've tried to look online but all of the examples that I have seen either don't use Jackson or only are for a single JSON object like so:

HashMap<String, String>[] values = new ObjectMapper().readValue(jsonString, new TypeReference<HashMap<String, String>[]>() {});

我也看过Gson的例子,我知道我可以做一些字符串解析魔术:

I've also seen Gson examples and I know I can do some string parsing magic:

jsonSting = jsonString.substring(jsonString.indexOf("["), (jsonString.indexOf("]")+1))

以我可以使用的格式获取它,但我想与Jackson一起尝试以避免导入另一个图书馆有任何想法吗?

to get it in a format that I can use, but I want to try it with Jackson to avoid importing another library. Any ideas?

重新提出问题:
所以如果我只有这样的客户列表:

Rephrasing the question:So if I only had a list of clients like so:

jsonString = [{"name" : "client 1","id" : 1},{"name" : "client 2","id" : 2}]

然后我可以做:

HashMap[] values = new ObjectMapper().readValue(jsonString, new TypeReference[]>() {});

得到我想要的东西。我基本上问是否有一种方法使用Jackson方法从顶部的大JSON部分获取上面的jsonString。我知道我可以通过字符串解析这个例子很容易地做到这一点,但将来会有更复杂的情况,字符串解析并不是真正的最佳实践

to get what I want. I am basically asking if there is a way using Jackson methods to get the jsonString above from the large JSON section on top. I know I can easily do it with this example with string parsing but there will be more complex situations in the future and string parsing is not really considered best practice

推荐答案

您可以使用提取JSON树的一部分,然后将其转换为地图数组。

You can extract a part of the JSON tree using the Jackson tree model API and then convert it to an array of maps.

以下是一个示例:

public class JacksonReadPart {
    public static final String JSON = "{\n" +
            "   \"clients\" : [\n" +
            "   {\n" +
            "      \"name\" : \"client 1\",\n" +
            "      \"id\" : 1\n" +
            "   },\n" +
            "   {\n" +
            "      \"name\" : \"client 2\",\n" +
            "      \"id\" : 2\n" +
            "   }\n" +
            "],\n" +
            "   \"other\" : {\n" +
            "      \"something\" : \"\"\n" +
            "   }\n" +
            "\n" +
            "}";

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode node = mapper.readTree(JSON).path("clients");

        // non type safe
        Map<String, Object>[] clients = mapper.treeToValue(node, Map[].class);
        System.out.println(Arrays.toString(clients));

        // type safe
        JsonParser parser = mapper.treeAsTokens(node);
        clients = parser.readValueAs(new TypeReference<Map<String, Object>[]>() {});
        System.out.println(Arrays.toString(clients));
    }
}

输出:

[{name=client 1, id=1}, {name=client 2, id=2}]
[{name=client 1, id=1}, {name=client 2, id=2}]

这篇关于使用Jackson-Java将JSON转换为哈希映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:42