实例:fastjson方便一步一步分析每个json节点,实例:
void work() {
Teacher teacher = new Teacher();
String s = JSON.toJSONString(teacher);
System.out.println(s);
Teacher teacher1 = JSON.parseObject(s, Teacher.class);
JSONObject jsonObject = JSON.parseObject(s);
JSONArray students = jsonObject.getJSONArray("students");
List<Student> students1 = students.toJavaList(Student.class);
}
package com.zjzy.tuyingdata.tuyingdata.model.dto;
import com.zjzy.tuyingdata.tuyingdata.runner.TestRunner;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class Teacher {
private String name;
private List<Student> students;
public Teacher(){
name="tttt";
students=new ArrayList<>();
students.add(new Student());
}
}
package com.zjzy.tuyingdata.tuyingdata.model.dto;
import lombok.Data;
import java.util.Date;
@Data
public class Student {
private String id;
Student(){
id=new Date().toString();
}
}
https://www.cnblogs.com/ciweiyu/p/13649685.html
JSONArray封装数组对象
JSONObject封装Map对象
-
fastjson
//对象-->字符串 String str = JSON.toJSONString(user); //字符串-->对象 User user_2 = JSON.parseObject(jsonData, User.class);
-
Gson
//对象 --> 字符串 Gson gson = new Gson(); String str = gson.toJson(user); //字符串 --> 对象 Gson gson = new Gson(); User user_2 = gson.fromJson(jsonData, User.class);
-
jackson
//对象-->String ObjectMapper objectMapper = new ObjectMapper(); String str_3 = objectMapper.writeValueAsString(user); //String-->对象 User user_3 = objectMapper.readValue(jsonData, User.class);
1|3对象集合与字符串之间的互转
-
fastjson
//对象集合-->字符串 String users = JSON.toJSONString(userList); //字符串-->对象集合 List<User> userList = JSON.parseArray(userList, User.class);
-
Gson
//对象集合 --> 字符串 Gson gson=new Gson(); String users=gson.toJson(list); //字符串 --> 对象集合 Gson gson = new Gson(); List<User> list = gson1.fromJson(userList, new TypeToken<List<User>>(){}.getType());
-
jackson
//对象集合 --> 字符串 ObjectMapper objectMapper = new ObjectMapper(); String users_3 = objectMapper.writeValueAsString(userList); //字符串 --> 对象集合 List<User> userList_3 = objectMapper.readValue(strList, new TypeReference<List<User>>() {});
1|4字符串互转JSON对象
-
fastjson
//String --> Json对象 JSONObject jsonObject = JSONObject.parseObject(jsonData); //Json --> String String jsonString = jsonObject.toJSONString();
-
Gson
//String --> Json对象 JsonObject jsonObject = new JsonParser().parse(jsonData).getAsJsonObject(); //JsonObject jsonObject_2 = gson.fromJson(jsonString_2, JsonObject.class); 错误方法 返回为空(非null) //Json对象 --> String Gson gson = new Gson(); String jsonString = gson.toJson(jsonObject);
-
jackson
//jackson //String --> Json对象 //暂未知,有知道的大佬请告知 //Json对象 --> String //用到fastJson格式的JSONObject ObjectMapper objectMapper = new ObjectMapper(); String jsonString_3 = objectMapper.writeValueAsString(jsonObject_1);
1|5Map与字符串之间互转
-
fastjson
//字符串 --> map Map map = JSONObject.parseObject(strList, Map.class); //map --> 字符串 String jsonString = JSON.toJSONString(map);
-
Gson
//字符串-->map Gson gson = new Gson(); Map map_2 = gson.fromJson(strList, Map.class); //map-->字符串 Gson gson = new Gson(); String jsonString_2 = gson.toJson(map);
-
jackson
//字符串 --> map ObjectMapper objectMapper = new ObjectMapper(); Map map_3 = objectMapper.readValue(strList, Map.class); //map --> 字符串 String jsonString_3 = objectMapper.writeValueAsString(map);
1|6Map 转 JSON对象
-
fastjson
//map转json对象 JSONObject json = new JSONObject(map); //json对象转Map Map map_1 = JSONObject.parseObject(strList, Map.class); //Map<String,Object> map_1 = (Map<String,Object>)jsonObject_1; 此方法也行
-
Gson
//map转json对象 Gson gson = new Gson(); String jsonString_2 = gson.toJson(map); //JsonObject jsonObject_2 = gson.fromJson(jsonString_2, JsonObject.class); 错误方法 返回为空(非null) JsonObject jsonObject_2 = new JsonParser().parse(jsonString_2).getAsJsonObject(); //json对象转Map Map map_2 = gson.fromJson(strList, Map.class); System.out.println("断点");
-
jackson
//map转json对象 //暂未知,有知道的大佬请告知 //json对象转Map ObjectMapper objectMapper = new ObjectMapper(); Map map_3 = objectMapper.readValue(strList, Map.class);