目录
    • 1.1.fastjson的介绍:
    • 1.2.fastjson的特点:
    • 1.3.fastjson的简单说明:
  • 2.fastjson的用法
    • 2.1.json格式字符串与json对象之间的转换
    • 2.2.json格式字符串与javabean之间的转换
    • 2.3.javabean与json对象间的之间的转换

      1.前言

      1.1.fastjson的介绍:

        json(javascript object notation)是一种轻量级的数据交换格式。主要采用键值对({"name": "json"})的方式来保存和表示数据。jsonjs对象的字符串表示法,它使用文本表示一个js对象的信息,本质上是一个字符串。

        json的处理器有很多,这里我介绍一下fastjson,fastjson是阿里的开源json解析库,可以解析json格式的字符串,支持将java bean序列化为json字符串,也可以从json字符串反序列化到javabean。是一个极其优秀的json框架,github地址:fastjson

      1.2.fastjson的特点:

      1.fastjson数度快,无论序列化和反序列化,都是当之无愧的fast
      2.功能强大(支持普通jdk类包括任意java bean class、collection、map、date或enum)
      3.零依赖(没有依赖其它任何类库)

      1.3.fastjson的简单说明:

      fastjson对于json格式字符串的解析主要用到了下面三个类:
      1.json:fastjson的解析器,用于json格式字符串与json对象及javabean之间的转换
      2.jsonobject:fastjson提供的json对象
      3.jsonarray:fastjson提供json数组对象

      2.fastjson的用法

      首先定义三个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}]}";

      2.1.json格式字符串与json对象之间的转换

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

      /**
       * json字符串-简单对象型到jsonobject的转换
       */
      @test
      public void testjsonstrtojsonobject() {
      
          jsonobject jsonobject = jsonobject.parseobject(json_obj_str);
      
          system.out.println("studentname:  " + jsonobject.getstring("studentname") + ":" + "  studentage:  "
                  + jsonobject.getinteger("studentage"));
      
      }
      
      /**
       * jsonobject到json字符串-简单对象型的转换
       */
      @test
      public void testjsonobjecttojsonstr() {
      
          //已知jsonobject,目标要转换为json字符串
          jsonobject jsonobject = jsonobject.parseobject(json_obj_str);
          // 第一种方式
          string jsonstring = jsonobject.tojsonstring(jsonobject);
      
          // 第二种方式
          //string jsonstring = jsonobject.tojsonstring();
          system.out.println(jsonstring);
      }

      2.1.3.复杂json格式字符串与jsonobject之间的转换

      /**
       * 复杂json格式字符串到jsonobject的转换
       */
      @test
      public void testcomplexjsonstrtojsonobject() {
      
          jsonobject jsonobject = jsonobject.parseobject(complex_json_str);
      
          string teachername = jsonobject.getstring("teachername");
          integer teacherage = jsonobject.getinteger("teacherage");
      
          system.out.println("teachername:  " + teachername + "   teacherage:  " + teacherage);
      
          jsonobject jsonobjectcourse = jsonobject.getjsonobject("course");
           //获取jsonobject中的数据
          string coursename = jsonobjectcourse.getstring("coursename");
          integer code = jsonobjectcourse.getinteger("code");
      
          system.out.println("coursename:  " + coursename + "   code:  " + code);
      
          jsonarray jsonarraystudents = jsonobject.getjsonarray("students");
      
          //遍历jsonarray
          for (object object : jsonarraystudents) {
      
              jsonobject jsonobjectone = (jsonobject) object;
              string studentname = jsonobjectone.getstring("studentname");
              integer studentage = jsonobjectone.getinteger("studentage");
      
              system.out.println("studentname:  " + studentname + "   studentage:  " + studentage);
          }
      }
      
      /**
       * 复杂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<t>类,由于其构造方法使用protected进行修饰,故创建其子类
          //student student = jsonobject.parseobject(json_obj_str, new typereference<student>() {});
      
          //第三种方式,使用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.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<t>类,由于其构造方法使用protected进行修饰,故创建其子类
          student student = jsonobject.parseobject(jsonobject.tojsonstring(), new typereference<student>() {});
          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<student> students = new arraylist<student>();
          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<t>类,由于其构造方法使用protected进行修饰,故创建其子类
          arraylist<student> students = jsonarray.parseobject(jsonarray.tojsonstring(),
                  new typereference<arraylist<student>>() {});
      
          system.out.println(students);
      
          //第二种方式,使用gson的思想
          list<student> 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<student> students = new arraylist<student>();
          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);
      
      }
      
      /**
       * 复杂json对象到javabean_obj的转换
       */
      @test
      public void testcomplexjsonobjecttojavabean() {
      
          //已知复杂json对象
          jsonobject jsonobject = jsonobject.parseobject(complex_json_str);
      
          //第一种方式,使用typereference<t>类,由于其构造方法使用protected进行修饰,故创建其子类
          teacher teacher = jsonobject.parseobject(jsonobject.tojsonstring(), new typereference<teacher>() {});
          system.out.println(teacher);
      
          //第二种方式,使用gson的思想
          teacher teacher1 = jsonobject.parseobject(jsonobject.tojsonstring(), teacher.class);
          system.out.println(teacher1);
      }

      总结

      // 把json文本parse为jsonobject或者jsonarray
              public static final object parse(string text); 
              
              // 把json文本parse成jsonobject
              public static final jsonobject parseobject(string text);
              
              // 把json文本parse为javabean
              public static final <t> t parseobject(string text, class<t> clazz); 
              
              // 把json文本parse成jsonarray
              public static final jsonarray parsearray(string text); 
              
              //把json文本parse成javabean集合
              public static final <t> list<t> parsearray(string text, class<t> clazz); 
              
              // 将javabean序列化为json文本
              public static final string tojsonstring(object object); 
              
              // 将javabean序列化为带格式的json文本
              public static final string tojsonstring(object object, boolean prettyformat); 
              
              //将javabean转换为jsonobject或者jsonarray。
              public static final object tojson(object javaobject);

      参考

      到此这篇关于java fastjson的简单用法的文章就介绍到这了,更多相关java fastjson使用内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!