1.對象與字符串之間的互轉
將對象轉換成為json字符串 String str = JSON.toJSONString(infoDo); json字符串轉換成為對象 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;