com.alibaba.fastjson.JSONObject之物件與JSON轉換方法
阿新 • • 發佈:2022-03-28
com.alibaba.fastjson.JSONObject時經常會用到它的轉換方法,包括Java物件轉成JSON串、JSON物件,JSON串轉成java物件、JSON物件,JSON物件轉換Java物件、JSON串等,使用方法總結如下,持續更新...
1、Java物件-->JSON物件
(JSONObject)JSONObject.toJSON(Java物件例項)
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 Student stu = new Student("公眾號BiggerBoy", "m", 2); 4 5 //Java物件轉化為JSON物件 6 JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); 7 System.out.println("Java物件轉化為JSON物件\n" + jsonObject);//{"name":"公眾號程式設計大道","age":2,"sex":"m"} 8 9 } 10 }
2、Java物件-->JSON字串
JSONObject.toJSONString(Java物件例項)
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 Student stu = new Student("公眾號BiggerBoy", "m", 2); 4 5 //Java物件轉換成JSON字串 6 String stuString = JSONObject.toJSONString(stu); 7 System.out.println("Java物件轉換成JSON字串\n" + stuString);//{"age":2,"name":"公眾號程式設計大道","sex":"m"} 8 } 9 }
3、JSON物件-->JSON字串
JSONObject.toJSONString();
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 Student stu = new Student("公眾號BiggerBoy", "m", 2); 4 5 //先轉成JSON物件 6 JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); 7 8 //JSON物件轉換為JSON字串 9 String jsonString = jsonObject.toJSONString(); 10 System.out.println("JSON物件轉換為JSON字串\n" + jsonString);//{"name":"公眾號程式設計大道","age":2,"sex":"m"} 11 12 } 13 }
4、JSON物件-->Java物件
JSONObject.toJavaObject(JSON物件例項, Java物件.class);
public class JSON2JavaTest{ public static void main(String[] args) { Student stu = new Student("公眾號BiggerBoy", "m", 2); //先轉成JSON物件 JSONObject jsonObject = (JSONObject) JSONObject.toJSON(stu); //JSON物件轉換成Java物件 Student student = JSONObject.toJavaObject(jsonObject, Student.class); System.out.println("JSON物件轉換成Java物件\n" + student);//Student{name='公眾號程式設計大道', sex='m', age=2} } }
5、JSON字串-->JSON物件
JSONObject.parseObject(JSON字串)
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 String stuString = "{\"age\":2,\"name\":\"公眾號 BiggerBoy\",\"sex\":\"m\"}"; 4 5 //JSON字串轉換成JSON物件 6 JSONObject jsonObject1 = JSONObject.parseObject(stuString); 7 System.out.println("JSON字串轉換成JSON物件\n" + jsonObject1);//{"sex":"m","name":"公眾號程式設計大道","age":2} 8 } 9 }
6、JSON字串-->Java物件
JSONObject.parseObject(JSON字串, Java物件.class);
1 public class JSON2JavaTest{ 2 public static void main(String[] args) { 3 String stuString = "{\"age\":2,\"name\":\"公眾號 BiggerBoy\",\"sex\":\"m\"}"; 4 5 //JSON字串轉換成Java物件 6 Student student1 = JSONObject.parseObject(stuString, Student.class); 7 System.out.println("JSON字串轉換成Java物件\n" + student1);//Student{name='公眾號程式設計大道', sex='m', age=2} 8 } 9 }
7、JSON字串-->List<Java物件>
List<T> parseArray(JSON字串, Java物件.class)
public class JSON2JavaTest{ public static void main(String[] args) { String stuString = "[{\"age\":2,\"name\":\"公眾號\",\"sex\":\"m\"},{\"age\":18,\"name\":\"BiggerBoy\",\"sex\":\"m\"}]"; //JSON字串轉換成Java物件 List<Student> studentList = JSONObject.parseArray(stuString, Student.class); System.out.println("JSON字串轉換成Java物件list\n" +studentList );
}
}
文章出處,感謝大佬帥氣的總結~