JSON各種轉(zhuǎn)換問題(json轉(zhuǎn)List,json轉(zhuǎn)對象等)
首先引入jar包:
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.83</version> </dependency>
JSON相關
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相關:
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進行處理
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設置一個屬性
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
辦法二:給實體類加一個注解
@JsonIgnoreProperties(ignoreUnknown = true)
到此這篇關于JSON各種轉(zhuǎn)換(json轉(zhuǎn)List,json轉(zhuǎn)對象等)的文章就介紹到這了,更多相關json轉(zhuǎn)List內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java利用EasyExcel解析動態(tài)表頭及導出實現(xiàn)過程
以前做導出功能,表頭和數(shù)據(jù)都是固定的,下面這篇文章主要給大家介紹了關于Java利用EasyExcel解析動態(tài)表頭及導出實現(xiàn)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12
Spring的RedisTemplate存儲的key和value有特殊字符的處理
這篇文章主要介紹了Spring的RedisTemplate存儲的key和value有特殊字符的處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Spingboot?JPA?CriteriaBuilder?如何獲取指定字段
這篇文章?主要介紹了Spingboot?JPA?CriteriaBuilder?如何獲取指定字段,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12

