栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换操作

fastJson对于json格式字符串的解析主要用到了一下三个类:

JSON:fastJson的解析器,用于JSON格式字符串与JSON对象及javaBean之间的转换。

JSONObject:fastJson提供的json对象。

JSONArray:fastJson提供json数组对象。

我们可以把JSONObject当成一个Map来看,只是JSONObject提供了更为丰富便捷的方法,方便我们对于对象属性的操作。我们看一下源码。

同样我们可以把JSONArray当做一个List,可以把JSONArray看成JSONObject对象的一个集合。

此外,由于JSONObject和JSONArray继承了JSON,所以说也可以直接使用两者对JSON格式字符串与JSON对象及javaBean之间做转换,不过为了避免混淆我们还是使用JSON。

首先定义三个json格式的字符串,作为我们的数据源。

//json字符串-简单对象型
private static final String JSON_OBJ_STR = "{"studentName":"lily","studentAge":12}";
//json字符串-数组类型
private static final String JSON_ARRAY_STR = "[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]";
//复杂格式json字符串
private static final String COMPLEX_JSON_STR = "{"teacherName":"crystall","teacherAge":27,"course":{"courseName":"english","code":1270},"students":[{"studentName":"lily","studentAge":12},{"studentName":"lucy","studentAge":15}]}"; 

示例1:JSON格式字符串与JSON对象之间的转换。

示例1.1-json字符串-简单对象型与JSONObject之间的转换

 
 public static void testJSonStrToJSONObject(){
 JSonObject jsonObject = JSON.parseObject(JSON_OBJ_STR);
 //JSonObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR); //因为JSONObject继承了JSON,所以这样也是可以的
 System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
 }

示例1.2-json字符串-数组类型与JSONArray之间的转换

 
 public static void testJSonStrToJSONArray(){
 JSonArray jsonArray = JSON.parseArray(JSON_ARRAY_STR);
 //JSonArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的
 //遍历方式1
 int size = jsonArray.size();
 for (int i = 0; i < size; i++){
  JSonObject jsonObject = jsonArray.getJSonObject(i);
  System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
 }
 //遍历方式2
 for (Object obj : jsonArray) {
  JSonObject jsonObject = (JSONObject) obj;
  System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));
 }
 }

示例1.3-复杂json格式字符串与JSONObject之间的转换

 
 public static void testComplexJSonStrToJSONObject(){
 JSonObject jsonObject = JSON.parseObject(COMPLEX_JSON_STR);
 //JSonObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
 String teacherName = jsonObject.getString("teacherName");
 Integer teacherAge = jsonObject.getInteger("teacherAge");
 JSonObject course = jsonObject.getJSonObject("course");
 JSonArray students = jsonObject.getJSonArray("students");
 }

示例2:JSON格式字符串与javaBean之间的转换。

首先,我们针对数据源所示的字符串,提供三个javaBean。

public class Student {
 private String studentName;
 private Integer studentAge;
 public String getStudentName() {
 return studentName;
 }
 public void setStudentName(String studentName) {
 this.studentName = studentName;
 }
 public Integer getStudentAge() {
 return studentAge;
 }
 public void setStudentAge(Integer studentAge) {
 this.studentAge = studentAge;
 }
}
public class Course {
 private String courseName;
 private Integer code;
 public String getCourseName() {
 return courseName;
 }
 public void setCourseName(String courseName) {
 this.courseName = courseName;
 }
 public Integer getCode() {
 return code;
 }
 public void setCode(Integer code) {
 this.code = code;
 }
}
public class Teacher {
 private String teacherName;
 private Integer teacherAge;
 private Course course;
 private List students;
 public String getTeacherName() {
 return teacherName;
 }
 public void setTeacherName(String teacherName) {
 this.teacherName = teacherName;
 }
 public Integer getTeacherAge() {
 return teacherAge;
 }
 public void setTeacherAge(Integer teacherAge) {
 this.teacherAge = teacherAge;
 }
 public Course getCourse() {
 return course;
 }
 public void setCourse(Course course) {
 this.course = course;
 }
 public List getStudents() {
 return students;
 }
 public void setStudents(List students) {
 this.students = students;
 }
}

json字符串与javaBean之间的转换推荐使用 TypeReference 这个类,使用泛型可以更加清晰,当然也有其它的转换方式,这里就不做探讨了。

示例2.1-json字符串-简单对象型与javaBean之间的转换

 
 public static void testJSonStrToJavaBeanObj(){
 Student student = JSON.parseObject(JSON_OBJ_STR, new TypeReference() {});
 //Student student1 = JSONObject.parseObject(JSON_OBJ_STR, new TypeReference() {});//因为JSONObject继承了JSON,所以这样也是可以的
 System.out.println(student.getStudentName()+":"+student.getStudentAge());
 }

示例2.2-json字符串-数组类型与javaBean之间的转换


 public static void testJSonStrToJavaBeanList(){
 ArrayList students = JSON.parseObject(JSON_ARRAY_STR, new TypeReference>() {});
 //ArrayList students1 = JSONArray.parseObject(JSON_ARRAY_STR, new TypeReference>() {});//因为JSONArray继承了JSON,所以这样也是可以的
 for (Student student : students) {
  System.out.println(student.getStudentName()+":"+student.getStudentAge());
 }
 }

示例2.3-复杂json格式字符串与与javaBean之间的转换

 
 public static void testComplexJSonStrToJavaBean(){

 Teacher teacher = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference() {});
 //Teacher teacher1 = JSON.parseObject(COMPLEX_JSON_STR, new TypeReference() {});//因为JSONObject继承了JSON,所以这样也是可以的
 String teacherName = teacher.getTeacherName();
 Integer teacherAge = teacher.getTeacherAge();
 Course course = teacher.getCourse();
 List students = teacher.getStudents();
 }

对于TypeReference,由于其构造方法使用 protected 进行修饰,所以在其他包下创建其对象的时候,要用其实现类的子类:new TypeReference() {}

此外的:

1,对于JSON对象与JSON格式字符串的转换可以直接用 toJSonString()这个方法。

2,javaBean与JSON格式字符串之间的转换要用到:JSON.toJSonString(obj);

3,javaBean与json对象间的转换使用:JSON.toJSON(obj),然后使用强制类型转换,JSONObject或者JSONArray。

       最后说一点,我们作为程序员,研究问题还是要仔细深入一点的。当你对原理了解的有够透彻,开发起来也就得心应手了,很多开发中的问题和疑惑也就迎刃而解了,而且在面对其他问题的时候也可做到触类旁通。当然在开发中没有太多的时间让你去研究原理,开发中要以实现功能为前提,可等项目上线的后,你有大把的时间或者空余的时间,你大可去刨根问底,深入的去研究一项技术,为觉得这对一名程序员的成长是很重要的事情。

总结

以上所述是小编给大家介绍的FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号