1.JSONObject对象?

  概念:Js对象。 

    提供了 toJSONString() 和 parseObject() 方法来将 Java 对象与 JSON 相互转换。调用toJSONString方 法即可将对象转换成 JSON 字符串,parseObject 方法则反过来将 JSON 字符串转换成对象。

  常用方法:JSONObject.tojsonString(jsonobject对象):将js对象转成js对象字符串表示方式;

         JSONObject.parseobject(js字符串):将js字符串转成js对象;

       JSONObject对象介绍?JSONObject对象转字符串,_js对象

2.创建json对象的几种方式?

  2.1介绍:

    a.使用原生的方式创建 JSONObject jsonobject = new JSONObject();

    b.创建一个实体类,然后将这个实体类转成jsonobject对象

    c.创建一个hashmap,并赋值,然后将这个hashmap转成jsonobject对象。

    d.通过js字符串转换生成js对象。

               e.在list集合里面添加对象,然后将list转成jsonobject对象。

  2.2代码示例:



1 package com.example.demo.jastjson;
2
3 import com.alibaba.fastjson.JSON;
4 import com.alibaba.fastjson.JSONArray;
5 import com.alibaba.fastjson.JSONException;
6 import com.alibaba.fastjson.JSONObject;
7 import com.example.demo.pojo.Student;
8 import com.example.demo.pojo.Student1;
9 import org.junit.jupiter.api.Test;
10
11 import java.util.ArrayList;
12 import java.util.HashMap;
13
14 public class FastJsonDemo {
15
16 @Test
17 public void test01() throws Exception {
18
19
20 //方式1:创建原生jsonobject对象
21 JSONObject zhangsan = new JSONObject ( );
22 try {
23 //添加
24 zhangsan.put ("name", "张三");
25 zhangsan.put ("age", 18.4);
26 zhangsan.put ("birthday", "1900-20-03");
27 zhangsan.put ("majar", new String[]{"哈哈", "嘿嘿"});
28 zhangsan.put ("null", null);
29 zhangsan.put ("house", false);
30 System.out.println (zhangsan.toString ( ));
31 } catch (JSONException e) {
32 e.printStackTrace ( );
33 }
34
35
36 //方式2:通过hashMap创建jsonObject对象
37 HashMap<String, Object> zhangsan1 = new HashMap<> ( );
38 zhangsan1.put ("name", "张三");
39 zhangsan1.put ("age", 18.4);
40 zhangsan1.put ("birthday", "1900-20-03");
41 zhangsan1.put ("majar", new String[]{"哈哈", "嘿嘿"});
42 zhangsan1.put ("null", null);
43 zhangsan1.put ("house", false);
44 System.out.println (new JSONObject (zhangsan1).toString ( ));
45
46
47 //方式3:通过实体类生成
48 Student1 student = new Student1 ( );
49 student.setId (1L);
50 student.setAge ("20");
51 student.setName ("张三");
52 //生成json格式的Student的对象
53 System.out.println (JSON.toJSON (student));
54 //对象转成string
55 String stuString = JSONObject.toJSONString (student);
56 //jsonstring字符串转jsonobject对象
57 Object stu = JSONObject.parse (stuString);
58 System.out.println ("student 的Json字符串:" + stuString);
59 System.out.println ("stu=>>>>>" + stu);
60
61
62 //方式4:通过json字符串生成json对象
63 String studentString = "{\"id\":1,\"age\":2,\"name\":\"zhang\"}";
64 //JSON字符串转换成JSON对象
65 JSONObject jsonObject1 = JSONObject.parseObject (stuString);
66 System.out.println (jsonObject1);
67
68
69 //方式四:通过list里面添加对象,使用jSON.tojson方法,转成jsonObject,JsonObject转tojsonString方法(JSONObject.toString)
70 ArrayList<Student1> studentLsit = new ArrayList<> ( );
71 Student1 student1 = new Student1 ( );
72 student1.setId (1L);
73 student1.setAge ("20");
74 student1.setName ("asdasdasd");
75
76 studentLsit.add (student1);
77
78 Student1 student2 = new Student1 ( );
79 student2.setId (2l);
80 student2.setAge ("20");
81 student2.setName ("aaaa:;aaa");
82
83 studentLsit.add (student2);
84 System.out.println ("============================");
85 //list转json字符串
86 String string = JSON.toJSON (studentLsit).toString ( );
87 System.out.println (string);
88 System.out.println ("=============================");
89 //json字符串转listJson格式
90 JSONArray jsonArray = JSONObject.parseArray (string);
91
92 System.out.println (jsonArray);
93
94 //测试jsonobject
95 JSONObject jsonObject = new JSONObject ( );
96 jsonObject.put ("name", "王杰");
97 jsonObject.put ("age", "23");
98 jsonObject.put ("id", "1");
99 jsonObject.put ("address", "北京市");
100 jsonObject.put ("email", "22222@qq.com");
101
102 System.out.println ("jsonObject:" + jsonObject);
103 String string1 = JSONObject.toJSONString (jsonObject);
104 System.out.println ("jsonObjectString:" + string1);
105 System.out.println ("jsonString===>>jsonObject" + JSONObject.parseObject (string1));
106
107 }
108 }


3.JSONObject序列化和反序列化操作?



1 String text = JSON.toJSONString(obj); //序列化
2 VO vo = JSON.parseObject("{...}", VO.class); //反序列化


4.FastJson使用依赖?



<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>x.x.x</version>
</dependency>