当前位置:   article > 正文

Java基础22(JSON解析 注解)_java解析json数据

java解析json数据

目录

一、JSON解析

1. JSON语法

2. JSON的用途

3. Java解析JSON

4. 使用Fastjson 

4.1 Fastjson 的优点

4.2  Fastjson 导包

4.3 Fastjson的主要对象

4.4 常用方法

将Java对象 "序列化"(转换) 为JSON字符串:

将JSON字符串反序列化为Java对象:

将JSON字符串反序列化为JSONArray集合数组:

4.5 一些难点

二、注解

正常情况下会默认过滤null

控制JSON的字段顺序

控制JSON的Date字段格式


一、JSON解析

JSON是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的JavaScript规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。

1. JSON语法

  • 使用大括号 { } 保存对象,每个对象由若干数据组成
  • 每个数据由key:value键值对组成
  • 数据之间使用逗号 , 分隔
  • 使用 \ 进行特殊字符的转义

  • 使用中括号 [ ] 保存数组(集合),数组(集合)可以包含多个对象

2. JSON的用途

JSON做为一种轻量级的数据格式,它的用途主要是在计算机系统之间进行数据的传递。JSON作为数据传输的格式,有几个显著的优点:

  • JSON只允许使用UTF-8编码,不存在编码问题;
  • JSON内容仅包含key-value键值对,格式简单,不存在冗余结构,是一种轻量级结构;
  • 浏览器内置JSON支持,如果把数据用JSON发送给浏览器,可以用JavaScript直接处理;

所以,开发Web应用的时候,使用JSON作为数据传输,在浏览器端非常方便。因为JSON天生适合JavaScript处理,所以,绝大多数REST API都选择JSON作为数据传输格式。

3. Java解析JSON

在使用Java进行应用程序的开发中,我们会面临类似“将Java对象转换成JSON格式”或者“将JSON格式的数据转换成Java对象“的需求,所以我们需要掌握如何使用第三方库来进行JSON格式数据的解析。
常用的用于解析JSON的第三方库有:

  • Jackson
  • Gson
  • Fastjson
  • ...

4. 使用Fastjson 

fastjson 是阿里巴巴的开源JSON解析库,它可以解析 JSON 格式的字符串,支持将 Java Bean 序列化为 JSON 字符串,也可以从 JSON 字符串反序列化到 JavaBean。

4.1 Fastjson 的优点

  • 速度快:fastjson相对其他JSON库的特点是快,从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越。
  • 使用广泛:fastjson在阿里巴巴大规模使用,在数万台服务器上部署,fastjson在业界被广泛接受。在2012年被开源中国评选为最受欢迎的国产开源软件之一。
  • 测试完备:fastjson有非常多的testcase,在1.2.11版本中,testcase超过3321个。每次发布都会进行回归测试,保证质量稳定。
  • 使用简单:fastjson的 API 十分简洁。
  • 功能完备:支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展。

4.2  Fastjson 导包

右键new 创建lib文件夹 

将fastjson2-2.0.8.jar 复制进去 并右键添加路径

4.3 Fastjson的主要对象

fastjson 主要使用是JSON接口、JSONObject类、JSONArray类。

  • JSON接口:提供json解析操作的入口方法,用于原始转换。
  • JSONObject类 : 封装json格式的对象。
  • JSONArray 类: 封装json格式的集合

4.4 常用方法

Weather类:

  1. public class Weather {
  2. private String temperature; // 温度
  3. private String weather; // 天气
  4. private String wind; // 风力
  5. private String week; // 星期
  6. private String city; // 城市
  7. private String date_y; // 日期
  8. private String dressing_index; // 穿衣指数
  9. private String dressing_advice; // 穿衣建议
  10. private String uv_index; // 紫外线指数
  11. private String comfort_index; // 舒适指数
  12. private String wash_index; // 洗衣指数
  13. private String travel_index; // 旅行指数
  14. private String exercise_index; // 晨练指数
  15. private String drying_index; // 晾晒指数
  16. get/set方法省略..
  17. }

将Java对象 "序列化"(转换) 为JSON字符串:

●JSON.toJSONString(Object object)

测试1:JSON.toJSONString()java对象转成json格式的字符串

  1. Weather w1 = new Weather();
  2. w1.setCity("西安");
  3. w1.setWeather("炎热");
  4. w1.setWind("1级微风");
  5. //测试1:JSON.toJSONString()java对象转成json格式的字符串
  6. String jsonWeather = JSON.toJSONString(w1);
  7. System.out.println(jsonWeather);//{"city":"西安","weather":"炎热","wind":"1级微风"}

测试2:toJsonstring()将List集合或者数组,序列化成JSON格式的字符串

  1. List<Weather> list = new ArrayList<Weather>();
  2. Weather w1 = new Weather();
  3. w1.setCity("西安");
  4. w1.setWeather("炎热");
  5. w1.setWind("1级微风");
  6. Weather w2 = new Weather();
  7. w2.setCity("上海");
  8. w2.setWeather("舒适");
  9. w2.setWind("2级微风");
  10. list.add(w1);
  11. list.add(w2);
  12. //测试2:toJsonstring()将List集合或者数组,序列化成JSON格式的字符串
  13. String jsonWeather = JSON.toJSONString(list);
  14. System.out.println(jsonWeather);

测试3:

  1. Map<String, Weather> map = new HashMap<String, Weather>();
  2. Weather w1 = new Weather();
  3. w1.setCity("西安");
  4. w1.setWeather("炎热");
  5. w1.setWind("1级微风");
  6. Weather w2 = new Weather();
  7. w2.setCity("上海");
  8. w2.setWeather("舒适");
  9. w2.setWind("2级微风");
  10. map.put("城市1", w1);
  11. map.put("城市2", w2);
  12. // 测试3:
  13. String resultString = JSON.toJSONString(map);
  14. System.out.println(resultString);//{"城市1":{"city":"西安","weather":"炎热","wind":"1级微风"},"城市2":{"city":"上海","weather":"舒适","wind":"2级微风"}}
将JSON字符串反序列化为Java对象:

●JSoN.parseObject(json字符串)转成对象

测试1:将JSON字符串转JsonObject

  1. String jsonString = "{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"}";
  2. //将JSON字符串转JsonObject
  3. // JSON.parseObject(json字符串)---转JSONObject对象
  4. JSONObject object = JSON.parseObject(jsonString);
  5. System.out.println(object.get("city"));//西安
  6. System.out.println(object.get("weather"));//炎热
  7. System.out.println(object.get("wind"));//1级微风

测试2:将序列化的JSON字符串转成Java对象

  1. String jsonString = "{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"}";
  2. //将序列化的JSON字符串转成Java对象
  3. //参数1:序列化的JSON字符串,参数2:指定的java对象的类型
  4. Weather w1 = JSON.parseObject(jsonString,Weather.class);
  5. System.out.println(w1);//Weather [wind=1级微风, weather=炎热, city=西安]

测试3:万能公式

  1. String jsonString = "{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"}";
  2. //new TypeReference<T>(){}是万能的,可以转成javabean类型,在T处传入泛型
  3. Weather w2 = JSON.parseObject(jsonString,new TypeReference<Weather>() {});
  4. System.out.println(w2);//Weather [wind=1级微风, weather=炎热, city=西安]
将JSON字符串反序列化为JSONArray集合数组:

●JSON.parseArray(jsonstring)转字符串为指定JSONArray类型

  1. String jsonString = "[{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"},{\"city\":\"上海\",\"weather\":\"舒适\",\"wind\":\"2级微风\"}]";
  2. // JSON.parseArray(jsonstring)转字符串为指定JSONArray类型
  3. JSONArray jsonArray = JSON.parseArray(jsonString);
  4. for (int i = 0; i < jsonArray.size(); i++) {
  5. JSONObject object = jsonArray.getJSONObject(i);
  6. System.out.print(object.get("city") + " ");
  7. System.out.print(object.get("weather") + " ");
  8. System.out.print(object.get("wind"));
  9. }
  10. 输出结果:
  11. 西安 炎热 1级微风
  12. 上海 舒适 2级微风

●JSON.parseArray(jsonstring,Weather.class)转字符串为指定集合类型

  1. String jsonString = "[{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"},{\"city\":\"上海\",\"weather\":\"舒适\",\"wind\":\"2级微风\"}]";
  2. // JSON.parseArray(jsonstring,Weather.class)转字符串为指定集合类型
  3. List<Weather> list = JSON.parseArray(jsonString, Weather.class);
  4. for (Weather weather : list) {
  5. System.out.println(weather);
  6. }
  7. 输出结果:
  8. Weather [wind=1级微风, weather=炎热, city=西安]
  9. Weather [wind=2级微风, weather=舒适, city=上海]

●new TypeReference<T>(){}万能的,可以转成javabean对象,在T处传泛型

  1. String jsonString = "[{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"},{\"city\":\"上海\",\"weather\":\"舒适\",\"wind\":\"2级微风\"}]";
  2. //new TypeReference<T>(){}万能的,可以转成javabean对象,在T处传泛型
  3. Weather[] list1 = JSON.parseObject(jsonString, new TypeReference<Weather[]>() {
  4. });
  5. for (Weather weather : list1) {
  6. System.out.println(weather);
  7. }
  8. 输出结果:
  9. Weather [wind=1级微风, weather=炎热, city=西安]
  10. Weather [wind=2级微风, weather=舒适, city=上海]

4.5 一些难点

  1. //JSONObject:
  2. //get() -----Object(JSONObject)
  3. //getJSONObject()------JSONObject()
  4. //getJSONArray() ----- JSONArray()
  5. //getObject(,weath.class)-----javaBean()
  6. public class Demo06 {
  7. public static void main(String[] args) {
  8. String jsonString = "{\"城市1\":{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"},\"城市2\":{\"city\":\"齐齐哈尔\",\"weather\":\"舒适\",\"wind\":\"2级微风\"}}";
  9. //JSON.parseObject方法将JSON格式的字符串转成JSONObject对象
  10. JSONObject jsonObject = JSON.parseObject(jsonString);
  11. //get()获取JSONObject中值--返回值类型是Object
  12. JSONObject object1 = (JSONObject) jsonObject.get("城市1");
  13. //getJSONObject()获取JSONObject中的键对应的值----返回值类型jsonObject
  14. JSONObject object2 = jsonObject.getJSONObject("城市2");
  15. //getObject(key,类型)获取JSONObject中的键对应的值s--返回值类型是javaBean类型
  16. Weather w1 = jsonObject.getObject("城市1", Weather.class);
  17. System.out.println(object1.get("city"));
  18. System.out.println(object2.get("city"));
  19. System.out.println(w1);
  20. // 转回到Map类型
  21. //万能公式转成对应javabean类型
  22. Map<String, Weather> map1 = JSON.parseObject(jsonString, new TypeReference<Map<String, Weather>>() {
  23. });
  24. Weather weather1 = map1.get("城市1");
  25. Weather weather2 = map1.get("城市2");
  26. System.out.println(weather1.getCity());
  27. System.out.println(weather2.getCity());
  28. }
  29. }
  1. public class Demo07 {
  2. public static void main(String[] args) {
  3. // 解析json字符串
  4. String jsonString = jsonMethd();
  5. System.out.println(jsonString);
  6. // 转成JSONObject类型的对象
  7. JSONObject jsonObject = JSON.parseObject(jsonString);
  8. // 获取键对应的值,getJSONObject--JSONObject
  9. JSONObject object1 = jsonObject.getJSONObject("城市1");
  10. System.out.println(object1.get("city"));// 获取JSONObject中key对应的值
  11. // getObject()转成javabean对象
  12. // Weather w1=jsonObject.getObject("城市1", Weather.class);
  13. // System.out.println(w1.getCity());
  14. // 获取键对应的JSONArray对象,方法getJSONArray()
  15. JSONArray array1 = jsonObject.getJSONArray("未上榜的城市");
  16. for (int i = 0; i < array1.size(); i++) {
  17. JSONObject obj1 = array1.getJSONObject(i);
  18. System.out.println(obj1.get("city"));
  19. }
  20. // 转成对应的javabean类型的对象
  21. // List<Weather> list = JSON.parseArray(array1.toString(),Weather.class);
  22. // List<Weather> list = jsonObject.getList("未上榜的城市", Weather.class);
  23. // for (Weather weather : list) {
  24. // System.out.println(weather.getCity());
  25. // }
  26. }
  27. /**
  28. * @return 生成json字符串,{key:{},key:[{},{}]}
  29. */
  30. public static String jsonMethd() {
  31. Map<String, Object> map = new HashMap<String, Object>();
  32. List<Weather> list = new ArrayList<Weather>();
  33. Weather w1 = new Weather();
  34. w1.setCity("西安");
  35. w1.setWeather("炎热");
  36. w1.setWind("1级微风");
  37. Weather w2 = new Weather();
  38. w2.setCity("齐齐哈尔");
  39. w2.setWeather("舒适");
  40. w2.setWind("2级微风");
  41. list.add(w1);
  42. list.add(w2);
  43. map.put("城市1", w1);
  44. map.put("未上榜的城市", list);
  45. String jsonString = JSON.toJSONString(map);
  46. return jsonString;
  47. }
  48. }

二、注解

正常情况下会默认过滤null

解决:转换成JSON字符串时,使用Feature枚举值进行设置。

  1. Map<String, String> map = new HashMap<String, String>();
  2. map.put("姓名", "zkt");
  3. map.put("年龄", "21");
  4. map.put("收入", null);
  5. //正常情况下会默认过滤null
  6. String jsonResultString = JSON.toJSONString(map, Feature.WriteMapNullValue);
  7. System.out.println(jsonResultString);//{"姓名":"zkt","收入":null,"年龄":"21"}

常用枚举值:

Feature.WriteMapNullValue

如果Map中包含Null值,则输出,不会过滤

Feature.WriteNullListAsEmpty

如果输出的List值为Null,则输出[ ],不会输出Null

Feature.WriteNullStringAsEmpty

如果输出的字符串值为Null,则输出“”,不会输出Null

Feature.WriteNullNumberAsZero

如果输出的数字值为Null,则输出0,不会输出Null

Feature.UseSingleQuotes

使用单引号

  1. public class Demo02 {
  2. public static void main(String[] args) {
  3. User user = new User();
  4. user.setName("zkt");
  5. user.setDate(new Date());
  6. String jsonResult = JSON.toJSONString(user,Feature.WriteMapNullValue,
  7. Feature.UseSingleQuotes,
  8. Feature.WriteNullListAsEmpty,
  9. Feature.WriteNullNumberAsZero,
  10. Feature.WriteNullBooleanAsFalse,
  11. Feature.WriteNullStringAsEmpty);
  12. System.out.println(jsonResult);//{'姓名':'zkt','userAddress':[],'schoolString':'','flag':false,'date':null}
  13. }
  14. }

控制JSON的字段顺序

j解决:输出结果与字段定义顺不一致。需要在定义实体类字段时,使用@JSONField注解的ordinal属性进行顺序配置。

  1. @JSONField
  2. ordina1用来设置成员变量的顺序,值越小,越靠前
  3. name序列化后的字符串的名称
  4. serialize是否要序列化此字段
  5. format格式化日期

控制JSON的Date字段格式

解决:输出日期字段时,默认格式不符合需求时,可以在定义实体类的Date字段,使用@JSONField注解的format属性进行格式配置。

  1. public class User {
  2. @JSONField(ordinal = 1,name = "姓名")
  3. private String name;
  4. @JSONField(ordinal = 2,serialize = false)
  5. private Integer userLevel;
  6. @JSONField(ordinal = 3)
  7. private List<String> userAddress;
  8. @JSONField(ordinal = 4)
  9. private String schoolString;
  10. @JSONField(ordinal = 5)
  11. private Boolean flag;
  12. @JSONField(ordinal = 6,format = "yyyy年MM月dd日")
  13. private Date date;
  14. get/set方法省略..
  15. }

Demo02 输出结果:

{'姓名':'zkt','userAddress':[],'schoolString':'','flag':false,'date':'2024年05月23日'}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/834099
推荐阅读
相关标签
  

闽ICP备14008679号