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; 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM