当前位置: 移动技术网 > IT编程>开发语言>Java > FastJson对于JSON格式字符串、JSON对象及JavaBean之间的相互转换操作

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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

json:fastjson的解析器,用于json格式字符串与json对象及javabean之间的转换。

jsonobject:fastjson提供的json对象。

jsonarray:fastjson提供json数组对象。

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

同样我们可以把jsonarray当做一个list<object>,可以把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之间的转换

 /**
 * 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之间的转换

 /**
 * 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之间的转换

 /**
 * 复杂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<student> 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<student> getstudents() {
 return students;
 }
 public void setstudents(list<student> students) {
 this.students = students;
 }
}

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

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

 /**
 * json字符串-简单对象与javabean_obj之间的转换
 */
 public static void testjsonstrtojavabeanobj(){
 student student = json.parseobject(json_obj_str, new typereference<student>() {});
 //student student1 = jsonobject.parseobject(json_obj_str, new typereference<student>() {});//因为jsonobject继承了json,所以这样也是可以的
 system.out.println(student.getstudentname()+":"+student.getstudentage());
 }

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

/**
 * json字符串-数组类型与javabean_list之间的转换
 */
 public static void testjsonstrtojavabeanlist(){
 arraylist<student> students = json.parseobject(json_array_str, new typereference<arraylist<student>>() {});
 //arraylist<student> students1 = jsonarray.parseobject(json_array_str, new typereference<arraylist<student>>() {});//因为jsonarray继承了json,所以这样也是可以的
 for (student student : students) {
  system.out.println(student.getstudentname()+":"+student.getstudentage());
 }
 }

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

 /**
 * 复杂json格式字符串与javabean_obj之间的转换
 */
 public static void testcomplexjsonstrtojavabean(){

 teacher teacher = json.parseobject(complex_json_str, new typereference<teacher>() {});
 //teacher teacher1 = json.parseobject(complex_json_str, new typereference<teacher>() {});//因为jsonobject继承了json,所以这样也是可以的
 string teachername = teacher.getteachername();
 integer teacherage = teacher.getteacherage();
 course course = teacher.getcourse();
 list<student> students = teacher.getstudents();
 }

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

此外的:

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

2,javabean与json格式字符串之间的转换要用到:json.tojsonstring(obj);

3,javabean与json对象间的转换使用:json.tojson(obj),然后使用强制类型转换,jsonobject或者jsonarray。

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

总结

以上所述是小编给大家介绍的fastjson对于json格式字符串、json对象及javabean之间的相互转换,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网