fastjson对象,JSON,字符串,map之间的互转

1.对象与字符串之间的互转

将对象转换成为字符串
String str = JSON.toJSONString(infoDo);
字符串转换成为对象
InfoDo infoDo = JSON.parseObject(strInfoDo, InfoDo.class);

2.对象集合与字符串之间的互转

将对象集合转换成为字符串
String users = JSON.toJSONString(users);
将字符串转换成为对象集合
List<User> userList = JSON.parseArray(userStr, User.class);  

3.字符串互转JSONObject

String 转 Json对象
JSONObject jsonObject = JSONObject.parseObject(jsonString);
json对象转string
JSONObject jsonObject = JSONObject.parseObject(str);//json对象转字符串
String jsonString = jsonObject.toJSONString();

4.map与字符串之间互转

  //字符串转map
  JSONObject  jsonObject = JSONObject.parseObject(str);
  Map<String,Object> map = (Map<String,Object>)jsonObject;//    //json对象转Map
  //map转字符串
  String jsonString = JSON.toJSONString(map);

5.Map 转 Json对象

   //map转json对象
Map<String,Object> map = new HashMap<>(); map.put("age", 24); map.put("name", "cool_summer_moon"); JSONObject json = new JSONObject(map);
  //json对象转Map
  Map<String,Object> map = (Map<String,Object>)jsonObject;

6.map互转对象

7.json对象互转对象

 

原文地址:https://www.cnblogs.com/heqiyoujing/p/9840424.html