003.Gson方式
package com.jdk.json; import com.google.gson.Gson; import com.jkd.entity.School; public class Gson_Json { public static String jsonString = "{\n" + " \"name\": \"teacher\",\n" + " \"computer\": {\n" + " \"CPU\": \"intel7\",\n" + " \"disk\": \"512G\"\n" + " },\n" + " \"students\": [\n" + " {\n" + " \"name\": \"张三\",\n" + " \"age\": 18,\n" + " \"sex\": \"男\"\n" + " },\n" + " {\n" + " \"name\": \"李四\",\n" + " \"age\": 19,\n" + " \"sex\": \"男\"\n" + " }\n" + " ]\n" + "}\n"; public static void main(String[] args) { //Json转换为java对象 Gson gson = new Gson(); School school = gson.fromJson(jsonString, School.class); System.out.println(school); //java对象转换为Json String s = gson.toJson(school); System.out.println(s); } }