Java基础22(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类:

public class Weather {
	private String temperature; // 温度
	private String weather; // 天气
	private String wind; // 风力
	private String week; // 星期
	private String city; // 城市

	private String date_y; // 日期

	private String dressing_index; // 穿衣指数
	private String dressing_advice; // 穿衣建议
	private String uv_index; // 紫外线指数

	private String comfort_index; // 舒适指数

	private String wash_index; // 洗衣指数
	private String travel_index; // 旅行指数
	private String exercise_index; // 晨练指数
	private String drying_index; // 晾晒指数

    get/set方法省略..


}

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

●JSON.toJSONString(Object object)

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

Weather w1 = new Weather();
		w1.setCity("西安");
		w1.setWeather("炎热");
		w1.setWind("1级微风");

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

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

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

测试3:

Map<String, Weather> map = new HashMap<String, Weather>();

		Weather w1 = new Weather();
		w1.setCity("西安");
		w1.setWeather("炎热");
		w1.setWind("1级微风");

		Weather w2 = new Weather();
		w2.setCity("上海");
		w2.setWeather("舒适");
		w2.setWind("2级微风");

		map.put("城市1", w1);
		map.put("城市2", w2);

		// 测试3:
		String resultString = JSON.toJSONString(map);
		System.out.println(resultString);//{"城市1":{"city":"西安","weather":"炎热","wind":"1级微风"},"城市2":{"city":"上海","weather":"舒适","wind":"2级微风"}}
将JSON字符串反序列化为Java对象:

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

测试1:将JSON字符串转JsonObject

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

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

String jsonString = "{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"}";

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

测试3:万能公式

String jsonString = "{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"}";

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

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

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


输出结果:
西安 炎热 1级微风
上海 舒适 2级微风

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

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


输出结果:
Weather [wind=1级微风, weather=炎热, city=西安]
Weather [wind=2级微风, weather=舒适, city=上海]

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

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


输出结果:
Weather [wind=1级微风, weather=炎热, city=西安]
Weather [wind=2级微风, weather=舒适, city=上海]

4.5 一些难点

//JSONObject:
//get() -----Object(JSONObject)
//getJSONObject()------JSONObject()
//getJSONArray() ----- JSONArray()
//getObject(,weath.class)-----javaBean()
public class Demo06 {
	public static void main(String[] args) {
		String jsonString = "{\"城市1\":{\"city\":\"西安\",\"weather\":\"炎热\",\"wind\":\"1级微风\"},\"城市2\":{\"city\":\"齐齐哈尔\",\"weather\":\"舒适\",\"wind\":\"2级微风\"}}";

//JSON.parseObject方法将JSON格式的字符串转成JSONObject对象
		JSONObject jsonObject = JSON.parseObject(jsonString);

//get()获取JSONObject中值--返回值类型是Object
		JSONObject object1 = (JSONObject) jsonObject.get("城市1");

//getJSONObject()获取JSONObject中的键对应的值----返回值类型jsonObject
		JSONObject object2 = jsonObject.getJSONObject("城市2");

//getObject(key,类型)获取JSONObject中的键对应的值s--返回值类型是javaBean类型
		Weather w1 = jsonObject.getObject("城市1", Weather.class);

		System.out.println(object1.get("city"));
		System.out.println(object2.get("city"));
		System.out.println(w1);

// 转回到Map类型
//万能公式转成对应javabean类型
		Map<String, Weather> map1 = JSON.parseObject(jsonString, new TypeReference<Map<String, Weather>>() {
		});
		Weather weather1 = map1.get("城市1");
		Weather weather2 = map1.get("城市2");

		System.out.println(weather1.getCity());
		System.out.println(weather2.getCity());

	}

}
public class Demo07 {
	public static void main(String[] args) {
		// 解析json字符串
		String jsonString = jsonMethd();
		System.out.println(jsonString);

		// 转成JSONObject类型的对象
		JSONObject jsonObject = JSON.parseObject(jsonString);

		// 获取键对应的值,getJSONObject--JSONObject
		JSONObject object1 = jsonObject.getJSONObject("城市1");
		System.out.println(object1.get("city"));// 获取JSONObject中key对应的值

//		getObject()转成javabean对象
//		Weather w1=jsonObject.getObject("城市1", Weather.class);
//		System.out.println(w1.getCity());

		// 获取键对应的JSONArray对象,方法getJSONArray()
		JSONArray array1 = jsonObject.getJSONArray("未上榜的城市");
		for (int i = 0; i < array1.size(); i++) {
			JSONObject obj1 = array1.getJSONObject(i);
			System.out.println(obj1.get("city"));
		}

//		转成对应的javabean类型的对象
//		List<Weather> list = JSON.parseArray(array1.toString(),Weather.class);
//		List<Weather> list = jsonObject.getList("未上榜的城市", Weather.class);
//		for (Weather weather : list) {
//			System.out.println(weather.getCity());
//		}
	}

	/**
	 * @return 生成json字符串,{key:{},key:[{},{}]}
	 */
	public static String jsonMethd() {
		Map<String, Object> map = new HashMap<String, Object>();

		List<Weather> list = new ArrayList<Weather>();
		Weather w1 = new Weather();
		w1.setCity("西安");
		w1.setWeather("炎热");
		w1.setWind("1级微风");

		Weather w2 = new Weather();
		w2.setCity("齐齐哈尔");
		w2.setWeather("舒适");
		w2.setWind("2级微风");

		list.add(w1);
		list.add(w2);

		map.put("城市1", w1);
		map.put("未上榜的城市", list);

		String jsonString = JSON.toJSONString(map);
		return jsonString;
	}

}

二、注解

正常情况下会默认过滤null

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

Map<String, String> map = new HashMap<String, String>();
		map.put("姓名", "zkt");
		map.put("年龄", "21");
		map.put("收入", null);

		//正常情况下会默认过滤null
		String jsonResultString = JSON.toJSONString(map, Feature.WriteMapNullValue);
		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

使用单引号

public class Demo02 {
	public static void main(String[] args) {
		User user = new User();
		user.setName("zkt");
		user.setDate(new Date());
		String jsonResult = JSON.toJSONString(user,Feature.WriteMapNullValue,
				Feature.UseSingleQuotes,
				Feature.WriteNullListAsEmpty,
				Feature.WriteNullNumberAsZero,
				Feature.WriteNullBooleanAsFalse,
				Feature.WriteNullStringAsEmpty);
		System.out.println(jsonResult);//{'姓名':'zkt','userAddress':[],'schoolString':'','flag':false,'date':null}
	}
}

控制JSON的字段顺序

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

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

控制JSON的Date字段格式

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

public class User {
	@JSONField(ordinal = 1,name = "姓名")
	private String name;
	
	@JSONField(ordinal = 2,serialize = false)
	private Integer userLevel;
	
	@JSONField(ordinal = 3)
	private List<String> userAddress;
	
	@JSONField(ordinal = 4)
	private String schoolString;
	
	@JSONField(ordinal = 5)
	private Boolean flag;
	
	@JSONField(ordinal = 6,format = "yyyy年MM月dd日")
	private Date date;

    get/set方法省略..
}

Demo02 输出结果:

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

  • 32
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冯诺依曼转世

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值