JAVA 最全最细的fastjson使用介绍,带你透彻领悟JSON

/**

  • 复杂JSONObject到json格式字符串的转换

*/

@Test

public void testJSONObjectToComplexJSONStr() {

//复杂JSONObject,目标要转换为json字符串

JSONObject jsonObject = JSONObject.parseObject(COMPLEX_JSON_STR);

//第一种方式

//String jsonString = JSONObject.toJSONString(jsonObject);

//第二种方式

String jsonString = jsonObject.toJSONString();

System.out.println(jsonString);

}

2.2.JSON格式字符串与javaBean之间的转换

2.2.1.json字符串-简单对象型与javaBean之间的转换

/**

  • json字符串-简单对象到JavaBean之间的转换

*/

@Test

public void testJSONStrToJavaBeanObj() {

//第一种方式

JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);

String studentName = jsonObject.getString(“studentName”);

Integer studentAge = jsonObject.getInteger(“studentAge”);

//Student student = new Student(studentName, studentAge);

//第二种方式,使用TypeReference类,由于其构造方法使用protected进行修饰,故创建其子类

//Student student = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference() {});

//第三种方式,使用Gson的思想

Student student = JSONObject.parseObject(JSON_OBJ_STR, Student.class);

System.out.println(student);

}

/**

  • JavaBean到json字符串-简单对象的转换

*/

@Test

public void testJavaBeanObjToJSONStr() {

Student student = new Student(“lily”, 12);

String jsonString = JSONObject.toJSONString(student);

System.out.println(jsonString);

}

2.2.2.json字符串-数组类型与javaBean之间的转换

/**

  • json字符串-数组类型到JavaBean_List的转换

*/

@Test

public void testJSONStrToJavaBeanList() {

//第一种方式

JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);

//遍历JSONArray

List students = new ArrayList();

Student student = null;

for (Object object : jsonArray) {

JSONObject jsonObjectone = (JSONObject) object;

String studentName = jsonObjectone.getString(“studentName”);

Integer studentAge = jsonObjectone.getInteger(“studentAge”);

student = new Student(studentName,studentAge);

students.add(student);

}

System.out.println("students: " + students);

//第二种方式,使用TypeReference类,由于其构造方法使用protected进行修饰,故创建其子类

List studentList = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference<ArrayList>() {});

System.out.println("studentList: " + studentList);

//第三种方式,使用Gson的思想

List studentList1 = JSONArray.parseArray(JSON_ARRAY_STR, Student.class);

System.out.println("studentList1: " + studentList1);

}

/**

  • JavaBean_List到json字符串-数组类型的转换

  • @JSONField(serialize = false)在实体类,用这个注解在上面注一下,转换出来的json字符串会没有对

  • 应的字段。

*/

@Test

public void testJavaBeanListToJSONStr() {

Student student = new Student(“lily”, 12);

Student studenttwo = new Student(“lucy”, 15);

List students = new ArrayList();

students.add(student);

students.add(studenttwo);

String jsonString = JSONArray.toJSONString(students);

System.out.println(jsonString);

}

2.2.3.复杂json格式字符串与与javaBean之间的转换

/**

  • 复杂json格式字符串到JavaBean_obj的转换

*/

@Test

public void testComplexJSONStrToJavaBean(){

//第一种方式,使用TypeReference类,由于其构造方法使用protected进行修饰,故创建其子类

Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference() {});

System.out.println(teacher);

//第二种方式,使用Gson思想

Teacher teacher1 = JSONObject.parseObject(COMPLEX_JSON_STR, Teacher.class);

System.out.println(teacher1);

}

/**

  • 复杂JavaBean_obj到json格式字符串的转换

*/

@Test

public void testJavaBeanToComplexJSONStr(){

//已知复杂JavaBean_obj

Teacher teacher = JSONObject.parseObject(COMPLEX_JSON_STR, new TypeReference() {});

String jsonString = JSONObject.toJSONString(teacher);

System.out.println(jsonString);

}

2.3.javaBean与json对象间的之间的转换

2.3.1.简单javaBean与json对象之间的转换

/**

  • 简单JavaBean_obj到json对象的转换

*/

@Test

public void testJavaBeanToJSONObject(){

//已知简单JavaBean_obj

Student student = new Student(“lily”, 12);

//方式一

String jsonString = JSONObject.toJSONString(student);

JSONObject jsonObject = JSONObject.parseObject(jsonString);

System.out.println(jsonObject);

//方式二

JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(student);

System.out.println(jsonObject1);

}

/**

  • 简单json对象到JavaBean_obj的转换

*/

@Test

public void testJSONObjectToJavaBean(){

//已知简单json对象

JSONObject jsonObject = JSONObject.parseObject(JSON_OBJ_STR);

//第一种方式,使用TypeReference类,由于其构造方法使用protected进行修饰,故创建其子类

Student student = JSONObject.parseObject(jsonObject.toJSONString(), new TypeReference() {});

System.out.println(student);

//第二种方式,使用Gson的思想

Student student1 = JSONObject.parseObject(jsonObject.toJSONString(), Student.class);

System.out.println(student1);

}

2.3.2.JavaList与JsonArray之间的转换

/**

  • JavaList到JsonArray的转换

*/

@Test

public void testJavaListToJsonArray() {

//已知JavaList

Student student = new Student(“lily”, 12);

Student studenttwo = new Student(“lucy”, 15);

List students = new ArrayList();

students.add(student);

students.add(studenttwo);

//方式一

String jsonString = JSONArray.toJSONString(students);

JSONArray jsonArray = JSONArray.parseArray(jsonString);

System.out.println(jsonArray);

//方式二

JSONArray jsonArray1 = (JSONArray) JSONArray.toJSON(students);

System.out.println(jsonArray1);

}

/**

  • JsonArray到JavaList的转换

*/

@Test

public void testJsonArrayToJavaList() {

//已知JsonArray

JSONArray jsonArray = JSONArray.parseArray(JSON_ARRAY_STR);

//第一种方式,使用TypeReference类,由于其构造方法使用protected进行修饰,故创建其子类

ArrayList students = JSONArray.parseObject(jsonArray.toJSONString(),

new TypeReference<ArrayList>() {});

System.out.println(students);

//第二种方式,使用Gson的思想

List students1 = JSONArray.parseArray(jsonArray.toJSONString(), Student.class);

System.out.println(students1);

}

2.3.3.复杂JavaBean_obj与json对象之间的转换

/**

  • 复杂JavaBean_obj到json对象的转换

*/

@Test

public void testComplexJavaBeanToJSONObject() {

//已知复杂JavaBean_obj

Student student = new Student(“lily”, 12);

Student studenttwo = new Student(“lucy”, 15);

List students = new ArrayList();

students.add(student);

students.add(studenttwo);

Course course = new Course(“english”, 1270);

Teacher teacher = new Teacher(“crystall”, 27, course, students);

//方式一

String jsonString = JSONObject.toJSONString(teacher);

JSONObject jsonObject = JSONObject.parseObject(jsonString);

System.out.println(jsonObject);

//方式二

JSONObject jsonObject1 = (JSONObject) JSONObject.toJSON(teacher);

System.out.println(jsonObject1);

}

/**

最后

文章中涉及到的知识点我都已经整理成了资料,录制了视频供大家下载学习,诚意满满,希望可以帮助在这个行业发展的朋友,在论坛博客等地方少花些时间找资料,把有限的时间,真正花在学习上,所以我把这些资料,分享出来。相信对于已经工作和遇到技术瓶颈的朋友们,在这份资料中一定都有你需要的内容。

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

  • 29
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值