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,從基礎到項目,一應俱全。可以先注冊再學習,這樣就可以記錄學習進度咯!!!