1 package com.example.demo.json; 2 3 4 import java.util.Map; 5 6 import com.alibaba.fastjson.JSON; 7 import com.alibaba.fastjson.JSONArray; 8 import com.alibaba.fastjson.JSONObject; 9 import com.alibaba.fastjson.TypeReference; 10 import com.example.demo.common.Person; 11 12 13 public class JsonLib { 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 void main(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 void testComplexJSONStrToJSONObject(){ 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 void testJSONStrToJSONArray(){ 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 void testJSONStrToJSONObject(){ 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 void demoJson() { 80 /** 81 * 将 Json 形式的字符串转换为 Map 82 */ 83 String str = "{"name":"Tom","age":90}"; 84 JSONObject jsonObject = JSONObject.parseObject(str); 85 Map<String, String> params = JSONObject.parseObject(jsonObject.toString(), new TypeReference<Map<String, String>>(){}); 86 System.out.println(params); 87 88 /** 89 * 将 Json 形式的字符串转换为 JavaBean 90 */ 91 str = "{"id":"A001","name":"Jack"}"; 92 jsonObject = JSONObject.parseObject(str); 93 System.out.println(jsonObject); 94 Person person = JSON.parseObject(str, new TypeReference<Person>() {}); 95 System.out.println(person.toString()); 96 } 97 }
给大家推荐一个很好的自学网站,https://how2j.cn?p=77721,how2j,从基础到项目,一应俱全。可以先注册再学习,这样就可以记录学习进度咯!!!