当前位置:网站首页>Conversion between JSON, string and map

Conversion between JSON, string and map

2022-06-22 05:16:00 a good idea

1. Object to string 

 Convert objects to strings 
String str = JSON.toJSONString(infoDo);
 String to object 
InfoDo infoDo = JSON.parseObject(strInfoDo, InfoDo.class);

2. Object set and string conversion

 Convert the collection of objects to strings 
String users = JSON.toJSONString(users);
 Convert string to object collection 
List<User> userList = JSON.parseArray(userStr, User.class);  

3. String conversion JSONObject

String  turn  Json object 
JSONObject jsonObject = JSONObject.parseObject(jsonString);
json Object turn string
JSONObject jsonObject = JSONObject.parseObject(str);//json Object to string  
String jsonString = jsonObject.toJSONString();

4.map And string to string

// String rotation map
  JSONObject  jsonObject = JSONObject.parseObject(str);
  Map<String,Object> map = (Map<String,Object>)jsonObject;//    //json Object turn Map
  //map Turn the string 
  String jsonString = JSON.toJSONString(map);

5.Map turn Json object

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

原网站

版权声明
本文为[a good idea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220510552751.html