1 packagecom.example.demo.json;2
3
4 importjava.util.Map;5
6 importcom.alibaba.fastjson.JSON;7 importcom.alibaba.fastjson.JSONArray;8 importcom.alibaba.fastjson.JSONObject;9 importcom.alibaba.fastjson.TypeReference;10 importcom.example.demo.common.Person;11
12
13 public classJsonLib {14 //json字符串-简单对象型
15 private static final String JSON_OBJ_STR = "{\"studentName\":\"lily\",\"studentAge\":12}";16 //json字符串-数组类型
17 private static final String JSON_ARRAY_STR = "[{\"studentName\":\"lily\",\"studentAge\":12},{\"studentName\":\"lucy\",\"studentAge\":15}]";18 //复杂格式json字符串
19 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}]}";20 @SuppressWarnings("unchecked")21 public static voidmain(String[] args) {22 //demoJson();23
24 //testJSONStrToJSONObject();//json字符串转化对象25 //testJSONStrToJSONArray();//json数组转化json对象
26 testComplexJSONStrToJSONObject();//json对象嵌套json对象
27 }28
29 /**
30 * 复杂json格式字符串与JSONObject之间的转换31 */
32 public static voidtestComplexJSONStrToJSONObject(){33 System.out.println(COMPLEX_JSON_STR);34 JSONObject jsonObject =JSON.parseObject(COMPLEX_JSON_STR);35 //JSONObject jsonObject1 = JSONObject.parseObject(COMPLEX_JSON_STR);//因为JSONObject继承了JSON,所以这样也是可以的
36 System.out.println(jsonObject);37 String teacherName = jsonObject.getString("teacherName");38 Integer teacherAge = jsonObject.getInteger("teacherAge");39 JSONObject course = jsonObject.getJSONObject("course");40 JSONArray students = jsonObject.getJSONArray("students");41 System.out.println(teacherName+"------"+teacherAge+"===json对象===="+course+"----json数组----"+students);42 JSONArray jsonArray =JSON.parseArray(students.toString());43 System.out.println(jsonArray);44 }45
46 /**
47 * json字符串-数组类型与JSONArray之间的转换48 */
49 public static voidtestJSONStrToJSONArray(){50
51 JSONArray jsonArray =JSON.parseArray(JSON_ARRAY_STR);52 //JSONArray jsonArray1 = JSONArray.parseArray(JSON_ARRAY_STR);//因为JSONArray继承了JSON,所以这样也是可以的53
54 //遍历方式1
55 int size =jsonArray.size();56 for (int i = 0; i < size; i++){57 JSONObject jsonObject =jsonArray.getJSONObject(i);58 System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));59 }60
61 //遍历方式2
62 for(Object obj : jsonArray) {63 JSONObject jsonObject =(JSONObject) obj;64 System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));65 }66 }67
68 /**
69 * json字符串-简单对象型与JSONObject之间的转换70 */
71 public static voidtestJSONStrToJSONObject(){72
73 JSONObject jsonObject =JSON.parseObject(JSON_OBJ_STR);74 //JSONObject jsonObject1 = JSONObject.parseObject(JSON_OBJ_STR);//因为JSONObject继承了JSON,所以这样也是可以的
75
76 System.out.println(jsonObject.getString("studentName")+":"+jsonObject.getInteger("studentAge"));77
78 }79 public static voiddemoJson() {80 /**
81 * 将 Json 形式的字符串转换为 Map82 */
83 String str = "{\"name\":\"Tom\",\"age\":90}";84 JSONObject jsonObject =JSONObject.parseObject(str);85 Map params = JSONObject.parseObject(jsonObject.toString(), new TypeReference>(){});86 System.out.println(params);87
88 /**
89 * 将 Json 形式的字符串转换为 JavaBean90 */
91 str = "{\"id\":\"A001\",\"name\":\"Jack\"}";92 jsonObject =JSONObject.parseObject(str);93 System.out.println(jsonObject);94 Person person = JSON.parseObject(str, new TypeReference() {});95 System.out.println(person.toString());96 }97 }