【14】JSON

1. JSON

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

2. JSON 两种格式

2.1 JSON对象

  • JSON 存放的是键值对,键值对之间使用逗号(,)隔开;

  • 键和值都是String数据类型,并使用双引号;

      //JSON 对象
      var stu = {"login_name":"zhangsan","password":"123123"};
      alert(stu.login_name)
      alert(stu.password)
    

2.2 JSON数组

    //JSON数组
  	var stuArray = [
  			{"login_name":"zhangsan","password":"123123"},
  			{"login_name":"lisi","password":"123456"},
  			{"login_name":"wangwu","password":"000000"}
  		];
  	
  	alert(stuArray.length)
  	for(var i = 0;i<stuArray.length;i++){
  		alert(stuArray[i].login_name + " , " +stuArray[i].password)
  	}

3. Fastjson

3.1 阿里巴巴产品

3.2 Maven

	<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
	<dependency>
		<groupId>com.alibaba</groupId>
		<artifactId>fastjson</artifactId>
		<version>1.2.47</version>
	</dependency>

3.3 使用

	User user1 = new User(1, "张三","115224", "13258695858", "412264867@qq.com", "412264867",
				"2018-9-9", 1);
		
	List<User> list = new ArrayList<>();
	list.add(new User(1, "张三","115224", "13258695858", "412264867@qq.com", "412264867","2018-9-9", 1));
	list.add(new User(1, "张三","115224", "13258695858", "412264867@qq.com", "412264867","2018-9-9", 1));
	list.add(new User(1, "张三","115224", "13258695858", "412264867@qq.com", "412264867","2018-9-9", 1));
	list.add(new User(1, "张三","115224", "13258695858", "412264867@qq.com", "412264867","2018-9-9", 1));
	list.add(new User(1, "张三","115224", "13258695858", "412264867@qq.com", "412264867","2018-9-9", 1));
		
	//简单java类转json字符串
	String jsonStr = JSON.toJSONString(user1);
	System.out.println(jsonStr);
		
	//List<Object>转json字符串
	String jsonStr2 = JSON.toJSONString(list);
    System.out.println(jsonStr2);

4. Gson

4.1 Google产品

4.2 Maven

	<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
	<dependency>
		<groupId>com.google.code.gson</groupId>
		<artifactId>gson</artifactId>
		<version>2.8.2</version>
	</dependency>

4.3 使用

    // 使用new方法
    Gson gson = new Gson();
    
    // toJson 将bean对象转换为json字符串
    String jsonStr = gson.toJson(user, User.class);
    
    // fromJson 将json字符串转为bean对象
    Student user= gson.fromJson(jsonStr, User.class);
    
    // **序列化List**
    String jsonStr2 = gson.toJson(list);
    
    // **反序列化成List时需要使用到TypeToken getType()**
    List<User> retList = gson.fromJson(jsonStr2,new TypeToken<List<User>>(){}.getType());
整理以下内容: JSON upIdx:1JSON trust mode:2JSON upIdx:2JSON trust mode:2JSON upIdx:3JSON trust mode:2JSON upIdx:4JSON trust mode:2JSON upIdx:5JSON trust mode:2JSON upIdx:6JSON trust mode:2JSON upIdx:7JSON trust mode:2JSON upIdx:8JSON trust mode:2JSON upIdx:9JSON trust mode:2JSON upIdx:10JSON trust mode:2JSON upIdx:11JSON trust mode:2JSON upIdx:12JSON trust mode:2JSON upIdx:13JSON trust mode:2JSON upIdx:14JSON trust mode:2JSON upIdx:15JSON trust mode:2JSON upIdx:16JSON trust mode:2JSON upIdx:17JSON trust mode:2JSON upIdx:18JSON trust mode:2JSON upIdx:19JSON trust mode:2JSON upIdx:20JSON trust mode:2JSON upIdx:21JSON trust mode:2JSON upIdx:22JSON trust mode:2JSON upIdx:23JSON trust mode:2JSON upIdx:24JSON trust mode:2JSON upIdx:25JSON trust mode:2JSON upIdx:26JSON trust mode:2JSON upIdx:27JSON trust mode:2JSON upIdx:28JSON trust mode:2JSON upIdx:29JSON trust mode:2JSON upIdx:30JSON trust mode:2JSON upIdx:31JSON trust mode:2JSON upIdx:32JSON trust mode:2JSON upIdx:33JSON trust mode:2JSON upIdx:34JSON trust mode:2JSON upIdx:35JSON trust mode:2JSON upIdx:36JSON trust mode:2JSON upIdx:37JSON trust mode:2JSON upIdx:38JSON trust mode:2JSON upIdx:39JSON trust mode:2JSON upIdx:40JSON trust mode:2JSON upIdx:41JSON trust mode:2JSON upIdx:42JSON trust mode:2JSON upIdx:43JSON trust mode:2JSON upIdx:44JSON trust mode:2JSON upIdx:45JSON trust mode:2JSON upIdx:46JSON trust mode:2JSON upIdx:47JSON trust mode:2JSON upIdx:48JSON trust mode:2JSON upIdx:49JSON trust mode:2JSON upIdx:50JSON trust mode:2JSON upIdx:51JSON trust mode:2JSON upIdx:52JSON trust mode:2JSON upIdx:53JSON trust mode:2JSON upIdx:54JSON trust mode:2JSON upIdx:1JSON trust mode:2JSON upIdx:2JSON trust mode:2JSON upIdx:3JSON trust mode:2JSON upIdx:4JSON trust mode:2JSON upIdx:5JSON trust mode:2JSON upIdx:6JSON trust mode:2JSON upIdx:7JSON trust mode:2JSON upIdx:8JSON trust mode:2JSON upIdx:9JSON trust mode:2JSON upIdx:10JSON trust mode:2JSON upIdx:11JSON trust mode:2JSON upIdx:12JSON trust mode:2JSON upIdx:13JSON trust mode:2JSON upIdx:14JSON trust mode:2JSON upIdx:15JSON trust mode:2JSON upIdx:16JSON trust mode:2JSON upIdx:17JSON trust mode:2JSON upIdx:18JSON trust mode:2JSON upIdx:19JSON trust mode:2JSON upIdx:20JSON trust mode:2JSON upIdx:21JSON trust mode:2JSON upIdx:22JSON trust mode:2JSON upIdx:23JSON trust mode:2JSON upIdx:24JSON trust mode:2JSON upIdx:25JSON trust mode:2JSON upIdx:26JSON trust mode:2JSON upIdx:27JSON trust mode:2JSON upIdx:28JSON trust mode:2JSON upIdx:29JSON trust mode:2JSON upIdx:30JSON trust mode:2JSON upIdx:31JSON trust mode:2JSON upIdx:32JSON trust mode:2JSON upIdx:33JSON trust mode:2JSON upIdx:34JSON trust mode:2JSON upIdx:35JSON trust mode:2JSON upIdx:36JSON trust mode:2JSON upIdx:37JSON trust mode:2JSON upIdx:38JSON trust mode:2JSON upIdx:39JSON trust mode:2JSON upIdx:40JSON trust mode:2JSON upIdx:41JSON trust mode:2JSON upIdx:42JSON trust mode:2JSON upIdx:43JSON trust mode:2JSON upIdx:44JSON trust mode:2JSON upIdx:45JSON trust mode:2JSON upIdx:46JSON trust mode:2JSON upIdx:47JSON trust mode:2JSON upIdx:48JSON trust mode:2JSON upIdx:49JSON trust mode:2JSON upIdx:50JSON trust mode:2JSON upIdx:51JSON trust mode:2JSON upIdx:52JSON trust mode:2JSON upIdx:53JSON trust mode:2JSON upIdx:54JSON trust mode:2add upIdx:1 add trustMode:2add trustKey:QOS_PORT_COMMON:1add upIdx:2 add trustMode:2add trustKey:QOS_PORT_COMMON:2add upIdx:3 add trustMode:2add trustKey:QOS_PORT_COMMON:3add upIdx:4 add trustMode:2add trustKey:QOS_PORT_COMMON:4add upIdx:5 add trustMode:2add trustKey:QOS_PORT_COMMON:5add upIdx:6 add trustMode:2add trustKey:QOS_PORT_COMMON:6add upIdx:7 add trustMode:2add trustKey:QOS_PORT_COMMON:7add upIdx:8 add trustMode:2add trustKey:QOS_PORT_COMMON:8add upIdx:9 add trustMode:2add trustKey:QOS_PORT_COMMON:9add upIdx:10 add trustMode:2add trustKey:QOS_PORT_COMMON:10add upIdx:11 add trustMode:2add trustKey:QOS_PORT_COMMON:11add upIdx:12 add trustMode:2add trustKey:QOS_PORT_COMMON:12add upIdx:13 add trustMode:2add trustKey:QOS_PORT_COMMON:13add upIdx:14 add trustMode:2add trustKey:QOS_PORT_COMMON:14add upIdx:15 add trustMode:2add trustKey:QOS_PORT_COMMON:15add upIdx:16 add trustMode:2add trustKey:QOS_PORT_COMMON:16add upIdx:17 add trustMode:2add trustKey:QOS_PORT_COMMON:17add upIdx:18 [_qosPortMibUpdateCb:704]phyPortWithCfgNum:0 add trustMode:2add trustKey:QOS_PORT_COMMON:18add upIdx:19 add trustMode:2add trustKey:QOS_PORT_COMMON:19add upIdx:20 add trustMode:2add trustKey:QOS_PORT_COMMON:20add upIdx:21 add trustMode:2add trustKey:QOS_PORT_COMMON:21add upIdx:22 add trustMode:2add trustKey:QOS_PORT_COMMON:22add upIdx:23 add trustMode:2add trustKey:QOS_PORT_COMMON:23add upIdx:24 add trustMode:2add trustKey:QOS_PORT_COMMON:24add upIdx:25 add trustMode:2add trustKey:QOS_PORT_COMMON:25add upIdx:26 add trustMode:2add trustKey:QOS_PORT_COMMON:26add upIdx:27 add trustMode:2add trustKey:QOS_PORT_COMMON:27add upIdx:28 add trustMode:2add trustKey:QOS_PORT_COMMON:28add upIdx:29 add trustMode:2add trustKey:QOS_PORT_COMMON:29add upIdx:30 add trustMode:2add trustKey:QOS_PORT_COMMON:30add upIdx:31 add trustMode:2add trustKey:QOS_PORT_COMMON:31add upIdx:32 add trustMode:2add trustKey:QOS_PORT_COMMON:32add upIdx:33 add trustMode:2add trustKey:QOS_PORT_COMMON:33add upIdx:34 add trustMode:2add trustKey:QOS_PORT_COMMON:34add upIdx:35 add trustMode:2add trustKey:QOS_PORT_COMMON:35add upIdx:36 add trustMode:2add trustKey:QOS_PORT_COMMON:36add upIdx:37 add trustMode:2add trustKey:QOS_PORT_COMMON:37add upIdx:38 add trustMode:2add trustKey:QOS_PORT_COMMON:38add upIdx:39 add trustMode:2add trustKey:QOS_PORT_COMMON:39add upIdx:40 add trustMode:2add trustKey:QOS_PORT_COMMON:40add upIdx:41 add trustMode:2add trustKey:QOS_PORT_COMMON:41add upIdx:42 add trustMode:2add trustKey:QOS_PORT_COMMON:42add upIdx:43 add trustMode:2add trustKey:QOS_PORT_COMMON:43add upIdx:44 add trustMode:2add trustKey:QOS_PORT_COMMON:44add upIdx:45 add trustMode:2add trustKey:QOS_PORT_COMMON:45add upIdx:46 add trustMode:2add trustKey:QOS_PORT_COMMON:46add upIdx:47 add trustMode:2add trustKey:QOS_PORT_COMMON:47add upIdx:48 add trustMode:2add trustKey:QOS_PORT_COMMON:48add upIdx:49 add trustMode:2add trustKey:QOS_PORT_COMMON:49add upIdx:50 add trustMode:2add trustKey:QOS_PORT_COMMON:50add upIdx:51 add trustMode:2add trustKey:QOS_PORT_COMMON:51add upIdx:52 add trustMode:2add trustKey:QOS_PORT_COMMON:52add upIdx:53 add trustMode:2add trustKey:QOS_PORT_COMMON:53add upIdx:54 add trustMode:2add trustKey:QOS_PORT_COMMON:54
最新发布
12-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值