将JSON记录转换为LinkedHashMap< String,String>使用Jackson API [英] Convert JSON record to LinkedHashMap<String,String> using Jackson API

查看:115
本文介绍了将JSON记录转换为LinkedHashMap< String,String>使用Jackson API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON文件(它包含JSON对象数组.) 我试图逐个对象地阅读它. 我需要将每个对象都转换为键和值均为字符串的LinkedHashMap<String,String>.请注意,即使JSON对象包含非字符串值(Integer/Boolean),我也希望我的LinkedHashMap包含字符串.

I have a JSON file(it contains an array of JSON objects.) I am trying to read it object by object. Each object I need to convert it to a LinkedHashMap<String,String> where both the key and value are strings. Note that even if the JSON objects contain a non-string value(Integer/Boolean), I want my LinkedHashMap to contain a string.

这是我的JSON文件(films.json):

This is my JSON file (films.json):

[
  {
    "name": "Fight Club",
    "year": 1999,
  }
]

现在,它有1个对象.我想将其转换为LinkedHashMap<String,String>.

Now, this has 1 object. I want to convert it to a LinkedHashMap<String,String>.

因此对于上面的示例,我的LinkedHashMap应该包含(对于第一个JSON对象):

So for the above example, my LinkedHashMap should contain(for the 1st JSON object) :

名称"; :"Fight CLub"

"name" : "Fight CLub"

年" :"1999"

"year" : "1999"

请注意年份在LinkedHashMap中而不是Integer中是String.

Notice how the year is String in the LinkedHashMap and not Integer.

这是我尝试过的.

Map<String, Object> myLinkedHashMap;

JsonParser jsonParser = new JsonFactory().createParser(new File("films.json"));
jsonParser = new JsonFactory().createParser(new File(filePath));
jsonParser.nextToken();

ObjectMapper  mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

while(jsonParser.nextToken() != JsonToken.END_ARRAY){
    myLinkedHashMap  = mapper.readValue(jsonParser, LinkedHashMap.class);
}

变量myLinkedHashMap将包含我的JSON文件中对象的键/值对.

The variable myLinkedHashMap will contain a key/value pair for an object in my JSON file.

但是问题在于,对于JSON文件的年份",我在LinkedHashMap中获取了Integer,因为JSON文件还包含Integer.

But the problem is that for 'year' of the JSON file, I am getting Integer in the LinkedHashMap as the JSON file also contains Integer.

相反,我希望将Integer用作LinkedHashMap中的String.

Instead, I want the Integer as String in the LinkedHashMap.

请帮助我在LinkedHashMap中获取String,而不是Integer.

Please help me get String in the LinkedHashMap instead of Integer.

注意:该解决方案也应该对其他数据类型通用.

Note: The solution should be generic to other data types also.

因此,如果JSON对象包含布尔值true,则我的LinkedHashMap应该包含"true".

So if the JSON object contains boolean true, then my LinkedHashMap should contain "true".

推荐答案

您可以使用TypeFactoryconstructMapType方法构造地图类型,以确切地说明readValue方法中您需要什么.参见以下示例:

You can construct map type using TypeFactory and constructMapType method to tell exactly what do you need from readValue method. See below example:

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.MapType;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.util.Assert;

import java.io.File;
import java.util.LinkedHashMap;

public class JsonMapApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new JavaTimeModule());
        mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

        JsonParser jsonParser = mapper.getFactory().createParser(jsonFile);
        jsonParser.nextToken();

        MapType mapType = mapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, String.class);
        while (jsonParser.nextToken() != JsonToken.END_ARRAY) {
            LinkedHashMap<String, String> map = mapper.readValue(jsonParser, mapType);
            map.forEach((k, v) -> {
                Assert.isInstanceOf(String.class, v);
                System.out.println(k + " -> " + v + " (" + v.getClass().getName() + ")");
            });
        }
    }
}

上面的代码显示:

name -> Fight Club (java.lang.String)
year -> 1999 (java.lang.String)

这篇关于将JSON记录转换为LinkedHashMap&lt; String,String&gt;使用Jackson API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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