JSON各种转换问题(json转List,json转对象等)
作者:你可以叫我老白 时间:2021-09-28 08:53:45
首先引入jar包:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
JSON相关
1.json转对象
Student o = JSONObject.parseObject(jsonString, Student.class);
2.json转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.对象转json
JSON.toJSONString(user);
4.List转json
JSON.toJSONString(users);
JSONObject相关:
1.json转JSONObject方法
String json = "";
JSONObject jsonObject = JSON.parseObject(json);
2.JSONObject转json方法
jsonObject.getString(key);
3.JSONObject转List
JSONObject jsonObject = JSON.parseObject(json);
// 获取到我们的jsonobject参数,并toJSONString
String s = JSONArray.toJSONString(jsonObject.get("servers"));
// 将json字符串转换为集合对象(实体类就省略了啊)
List<AnswerCardVo> cardVos = JSONArray.parseArray(s, AnswerCardVo.class);
4.Map转JSONObject
//直接调用new方法
Map map1 = new HashMap();
map1.put("one",users1);
map1.put("two",users1);
JSONObject mapJsonObject =(JSONObject) JSONObject.toJSON(map1);
System.out.println(mapJsonObject);
以上内容是接收字段与json字段一致的时候,那么不一致的时候怎么处理?
com.fasterxml.jackson包
例:json串内容如下,要转成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中指定了要接收哪个的值,这样就可以正常转换了
对象:
public class HwInstance {
private String fault;
private String id;
@JsonProperty("OS-EXT-STS:task_state")
private String state;
@JsonProperty("cpu_options")
private Object options;
}
转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)
来源:https://blog.csdn.net/zth_killer/article/details/128695599
标签:json,List
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解spring mvc中url-pattern的写法
2023-11-11 07:30:58
java 非对称加密算法RSA实现详解
2023-11-25 07:41:36
Java 处理超大数类型之BigInteger案例详解
2021-06-20 15:36:38
Mybatis 动态SQL的几种实现方法
2023-11-10 12:15:15
java中的this引用及对象构造初始化
2023-03-07 09:38:17
![](https://img.aspxhome.com/file/2023/6/60996_0s.png)
Java编程调用微信接口实现图文信息推送功能
2023-11-25 07:20:47
Android实现的数字格式化用法示例
2023-09-30 09:03:28
c语言动态数组示例
2023-11-02 22:56:44
Java 高并发三:Java内存模型和线程安全详解
2021-10-24 07:04:13
![](https://img.aspxhome.com/file/2023/6/59966_0s.png)
JAVA ArrayList详细介绍(示例)
2023-02-18 22:30:56
![](https://img.aspxhome.com/file/2023/3/61143_0s.jpg)
mybatis-plus 如何判断参数是否为空并作为查询条件
2022-09-14 22:05:03
![](https://img.aspxhome.com/file/2023/8/61078_0s.jpg)
Java异步处理机制实例详解
2022-05-12 01:39:37
C#启动进程的几种常用方法
2023-06-18 04:13:48
Springboot与Maven多环境配置的解决方案
2023-11-29 08:53:58
![](https://img.aspxhome.com/file/2023/9/60679_0s.png)
Java并发编程面试之线程池
2023-11-11 10:58:33
![](https://img.aspxhome.com/file/2023/4/58704_0s.png)
springboot+springmvc+mybatis项目整合
2023-07-16 03:13:57
![](https://img.aspxhome.com/file/2023/1/61251_0s.png)
Java中Servlet的生命周期详解
2023-09-10 03:57:59
![](https://img.aspxhome.com/file/2023/7/61057_0s.png)
java开发RocketMQ之NameServer路由管理源码分析
2023-10-27 20:56:12
![](https://img.aspxhome.com/file/2023/6/61266_0s.png)
Java深入讲解instanceof关键字的使用
2023-02-27 13:05:05
![](https://img.aspxhome.com/file/2023/8/60998_0s.png)
Java源码解析之平衡二叉树
2023-11-29 11:16:40
![](https://img.aspxhome.com/file/2023/2/60532_0s.png)