欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等)

 更新時間:2023年03月06日 10:27:11   作者:你可以叫我老白  
這篇文章主要介紹了JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等),本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

首先引入jar包:

<dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.83</version>
</dependency>

JSON相關(guān)

1.json轉(zhuǎn)對象

Student o = JSONObject.parseObject(jsonString, Student.class);

2.json轉(zhuǎn)List

List<Student> studentList = JSONObject.parseArray(jsonString, Student.class);

或者

String jsonString = “[[1,"zhangsan","male",18,"Beijing"],[2,"lisi","female",18,"Shanghai"]]”
List<List<Object>> list = JSON.parseObject([jsonString], new TypeReference<List<List<Object>>>() {});

3.對象轉(zhuǎn)json

JSON.toJSONString(user);

4.List轉(zhuǎn)json

JSON.toJSONString(users);

JSONObject相關(guān):

1.json轉(zhuǎn)JSONObject方法

String json = "";
JSONObject jsonObject = JSON.parseObject(json);

2.JSONObject轉(zhuǎn)json方法

jsonObject.getString(key);

3.JSONObject轉(zhuǎn)List

JSONObject jsonObject = JSON.parseObject(json);
// 獲取到我們的jsonobject參數(shù),并toJSONString
String s = JSONArray.toJSONString(jsonObject.get("servers"));
// 將json字符串轉(zhuǎn)換為集合對象(實體類就省略了?。?
List<AnswerCardVo> cardVos = JSONArray.parseArray(s, AnswerCardVo.class);

4.Map轉(zhuǎn)JSONObject

//直接調(diào)用new方法

Map map1 = new HashMap();
        map1.put("one",users1);
        map1.put("two",users1);
        JSONObject mapJsonObject =(JSONObject) JSONObject.toJSON(map1);
        System.out.println(mapJsonObject);

以上內(nèi)容是接收字段與json字段一致的時候,那么不一致的時候怎么處理?

com.fasterxml.jackson包

例:json串內(nèi)容如下,要轉(zhuǎn)成List

注意:可以看到,該json串中屬性名是這樣的OS-EXT-STS:task_state,那么我們接收的bean就無法寫成這個屬性名去接收,所以需要在接收Bean中使用注解@JsonProperty進(jìn)行處理

json串

{
"count": 3,
"servers": [{
"fault": null,
"id": "5c1ac257-",
"OS-EXT-STS:task_state": null,
"cpu_options": {
"hw:cpu_threads": null
}
}]
}

可以看到注解@JsonProperty中指定了要接收哪個的值,這樣就可以正常轉(zhuǎn)換了

對象:

public class HwInstance {

private String fault;

private String id;

@JsonProperty("OS-EXT-STS:task_state")
private String state;
@JsonProperty("cpu_options")
private Object options;
}

轉(zhuǎn)List

方法一:

@Autowired
private ObjectMapper objectMapper;
String json = "";
JSONObject jsonObject = JSON.parseObject(json);
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, HwInstance.class);
List<HwInstance> instances = objectMapper.readValue(objectMapper.readTree(jsonObject.toJSONString()).get("servers").toString(), javaType);

方法二:

@Autowired
private ObjectMapper objectMapper;
String json = "";
List<HwInstance> instances = objectMapper.readValue(objectMapper.readTree(json).get("servers").toString(),new TypeReference<List<HwInstance>>(){});

注意:可能會遇到報錯的情況如下:

報錯信息:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field “isleader”

意思是json’中的字段與實體類不匹配,解決辦法如下:

辦法一:給objectMapper設(shè)置一個屬性

objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

辦法二:給實體類加一個注解

@JsonIgnoreProperties(ignoreUnknown = true)

到此這篇關(guān)于JSON各種轉(zhuǎn)換(json轉(zhuǎn)List,json轉(zhuǎn)對象等)的文章就介紹到這了,更多相關(guān)json轉(zhuǎn)List內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論