Json是一種輕量級的數據交換格式, 其語法格式很是簡單json
對象:{key : value, key: value...}
數組:[元素1, 元素2 ...]後端
在平時的開發中,咱們都須要使用Json在先後端之間進行數據交換,這裏咱們主要來看一下Java最方便快捷的解析Json的方式數組
String toJson(Object src)
將json轉爲對象<T> T fromJson(String json, Class<T> classOfT)
code
將json轉爲集合<T> T fromJson(String json, Type typeOfT)
對象
代碼示例:開發
/** * 將對象轉換爲json * @param o * @return */ public static String getJsonString(Object o){ return new Gson().toJson(o); } /** * 將json轉爲對象 * @param jsonString * @param clazz * @return */ public static Object getObjectFromJson(String jsonString, Class<?> clazz){ return new Gson().fromJson(jsonString, clazz); } /** * 將json轉爲集合 * @param jsonString * @param type * @return */ public static Object getObjectFromJson(String jsonString, Type type){ return new Gson().fromJson(jsonString, type); } public static void main(String[] args) { // 對象轉json String objectJson = getJsonString(new Student(18, "jason")); //結果:{"age":18,"name":"jason"} System.out.println(objectJson); // json轉對象 Student student = (Student) getObjectFromJson("{\"age\":18,\"name\":\"jason\"}", Student.class); //結果:Student{age=18, name='jason'} System.out.println(student); // List List<Student> list = new ArrayList<>(); list.add(new Student(18, "jason")); list.add(new Student(19, "lee")); // 集合轉json String listJson = getJsonString(list); //結果:[{"age":18,"name":"jason"}, {"age":19,"name":"lee"}] System.out.println(listJson); // Json轉List Type type = new TypeToken<ArrayList<Student>>() { }.getType(); ArrayList<Student> arrayList = (ArrayList<Student>) getObjectFromJson(listJson, type); // 結果:[Student{age=18, name='jason'}, Student{age=19, name='lee'}] System.out.println(arrayList.toString()); // HashMap HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("name", "jason"); hashMap.put("age", "18"); // HashMap轉json String mapJson = getJsonString(hashMap); // 結果:{"name":"jason","age":"18"} System.out.println(mapJson); // json轉HashMap Type mapType = new TypeToken<HashMap<String, String>>() {}.getType(); HashMap<String, String> map = (HashMap<String, String>) getObjectFromJson(mapJson, mapType); /** * 結果 * key:name, value:jason * key:age, value:18 */ for (Map.Entry<String, String> entry : map.entrySet()){ System.out.println("key:" + entry.getKey() + ", value:" + entry.getValue()); } }
static String toJSONString(Object object)
json轉對象static <T> T parseObject(String text, Class<T> clazz)
get
json轉Liststatic <T> List<T> parseArray(String text, Class<T> clazz)
hash
json轉Mapstatic <T> T parseObject(String text, TypeReference<T> type, Feature... features)
ast
代碼示例:class
/** * 對象轉json * @param o * @return */ public static String getJsonString(Object o){ return JSON.toJSONString(o); } /** * json轉對象 * @param jsonString * @param clazz * @return */ public static Object getObjectFromJson(String jsonString, Class<?> clazz){ return JSON.parseObject(jsonString, clazz); } /** * json轉list * @param jsonString * @param clazz * @return */ public static Object getArrayFromJson(String jsonString, Class<?> clazz){ return JSON.parseArray(jsonString, clazz); } /** * json轉map * @param jsonString * @param typeReference * @return */ public static Object getMapFromJson(String jsonString, TypeReference<?> typeReference){ return JSON.parseObject(jsonString, typeReference); } public static void main(String[] args) { // 對象轉json String objectJson = getJsonString(new Student(18, "jason")); //結果:{"age":18,"name":"jason"} System.out.println(objectJson); // json轉對象 Student student = (Student) getObjectFromJson(objectJson, Student.class); // 結果:Student{age=18, name='jason'} System.out.println(student); // List List<Student> list = new ArrayList<>(); list.add(new Student(18, "jason")); list.add(new Student(19, "lee")); // List轉json String listJson = getJsonString(list); //結果:[{"age":18,"name":"jason"},{"age":19,"name":"lee"}] System.out.println(listJson); // json轉List List<Student> students = (List<Student>) getArrayFromJson(listJson, Student.class); // 結果:[Student{age=18, name='jason'}, Student{age=19, name='lee'}] System.out.println(students.toString()); // Map HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("name", "jason"); hashMap.put("age", "18"); // Map轉json String mapJson = getJsonString(hashMap); // 結果:{"name":"jason","age":"18"} System.out.println(mapJson); // json轉HashMap TypeReference<HashMap<String, String>> typeReference = new TypeReference<HashMap<String, String>>() {}; HashMap<String, String> map = (HashMap<String, String>) getMapFromJson(mapJson, typeReference); /** * 結果 * key:name, value:jason * key:age, value:18 */ for (Map.Entry<String, String> entry : map.entrySet()){ System.out.println("key:" + entry.getKey() + ", value:" + entry.getValue()); } }