定义
JSON是一种轻量级的数据交换格式,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于阅读和编写,同时也易于解析和生成,并有效地提升网络传输效率。
语法
数据在名称/值对中
数据由逗号分隔
花括号保存对象
方括号保存数组
{ "employees": [ { "firstName":"John" , "lastName":"Doe" }, { "firstName":"Anna" , "lastName":"Smith" }, { "firstName":"Peter" , "lastName":"Jones" } ] }
实例分析
public static void jsonChange() throws IOException { /** * 方式1:传统方式 org.json * Json数据转换成实体对象存在缺陷,性能和功能不够完善 */ // JSONObject/JSONArray与String相互转换 String jsonStr = "{"age":25,"name":"小明","sex":"男"}"; JSONObject jsonObject = new JSONObject(jsonStr); System.out.println("name="+jsonObject.getString("name")); System.out.println("age="+jsonObject.getInt("age")); System.out.println("sex="+jsonObject.getString("sex")); System.out.println(jsonObject.toString()); jsonStr = "[{"age":25,"name":"小明","sex":"男"},{"age":20,"name":"小红","sex":"女"}]"; JSONArray jsonArray = new JSONArray(jsonStr); System.out.println(jsonArray.toString()); for (int i=0; i<jsonArray.length();i++){ jsonObject = (JSONObject) jsonArray.get(i); System.out.println(jsonObject.toString()); } // 对象转JSONObject/JSONArray(org.json是不支持JSONObject/JSONArray转对象) jsonObject = new JSONObject(new Person("小明",25,"男")); System.out.println(jsonObject); List<Person> list = new ArrayList<>(); list.add(new Person("小明",25,"男")); jsonArray = new JSONArray(list); System.out.println(jsonArray.toString()); /** * 方式2:fastjson * 复杂的实体类转换成Json数据存在缺陷,解析json的速度最优 */ // String转对象 jsonStr = "{"age":25,"name":"小明","sex":"男"}"; Person person = JSON.parseObject(jsonStr,Person.class); System.out.println(person.toString()); jsonStr = "[{"age":25,"name":"小明","sex":"男"},{"age":20,"name":"小红","sex":"女"}]"; list = JSON.parseArray(jsonStr,Person.class); System.out.println(list.toString()); // 对象转String jsonStr = JSON.toJSONString(person); System.out.println(jsonStr); jsonStr = JSON.toJSONString(list); System.out.println(jsonStr); /** * 方式3:Jackson * 复杂的Json数据转换成实体类存在缺陷,性能和功能优于传统方式 */ // String转对象 ObjectMapper mapper=new ObjectMapper(); jsonStr = "{"age":25,"name":"小明","sex":"男"}"; person = mapper.readValue(jsonStr, Person.class); System.out.println(person.toString()); jsonStr = "[{"age":25,"name":"小明","sex":"男"},{"age":20,"name":"小红","sex":"女"}]"; list = mapper.readValue(jsonStr,mapper.getTypeFactory().constructParametricType(ArrayList.class, Person.class)); System.out.println(list.toString()); // 对象转String jsonStr = mapper.writeValueAsString(person); System.out.println(jsonStr); jsonStr = mapper.writeValueAsString(list); System.out.println(jsonStr); /** * 方式4:Gson * 功能方面在几种方式中最优,性能方面不如Jackson方式 */ // String转对象 String json = "{"age":25,"name":"小明","sex":"男"}"; Gson gson = new Gson(); person = gson.fromJson(json,Person.class); System.out.println(person.toString()); json = "[{"age":25,"name":"小明","sex":"男"},{"age":20,"name":"小红","sex":"女"}]"; list = gson.fromJson(json,new TypeToken<ArrayList<Person>>(){}.getType()); System.out.println(list.toString()); // 对象转String json = gson.toJson(person); System.out.println(json); json = gson.toJson(list); System.out.println(json); }