JSON小结

json的语法格式

json对象有三种数据格式,分别如下:

类型语法解释
对象类型{name:value,name:value…}其中name是字符串类型,而value是任意类型
数组/集合类型[value,value,value…]其中value是任意类型
混合类型[{},{}… …] 或 {name:[]… …}合理包裹嵌套对象类型和数组类型

json格式

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>json01</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>

  </body>
  
  <script language="JavaScript">
	/**
	 * 案例一
	 *  var person={key:value,key:value}
	 *  
	 * class Person{
	 * 	  String firstname = "张";
	 *    String lastname = "三丰";
	 *    Integer age = 100;
	 * }
	 * 
	 * Person p = new Person();
	 * System.out.println(p.firstname);
	 */
	
	 //json的定义
	 var person = {"firstname":"张","lastname":"三丰","age":100};
	 
	 //json解析
	 alert(person.firstname);
	 alert(person.lastname);
	 alert(person.age);
	
  </script>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>json02</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>

</body>

<script language="JavaScript">
  	/**
	 * 案例二
	 *  [{key:value,key:value},{key:value,key:value}]
	 *  
	 */
	var json = [
					{"firstname":"张","lastname":"三丰","age":100},
					{"firstname":"张","lastname":"翠山","age":58},
					{"firstname":"张","lastname":"无忌","age":23}
	             ];
  	
  	for(var i=0;i<json.length;i++){
  		alert(json[i].lastname);
  	}
 
  </script>
</html>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>json03</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>

  </body>
  
  <script language="JavaScript">
   /**
	 * 案例三
	 * {
	 *   "param":[{key:value,key:value},{key:value,key:value}]
	 * }
	 *  
	 *  
	 */
	 
	 var json = {
			"baobao":[
			          	{"name":"小双","age":18,"addr":"扬州"},
			          	{"name":"建宁","age":18,"addr":"北京海淀"},
			          	{"name":"龙儿","age":38,"addr":"岛国"},
			          	{"name":"阿珂","age":17,"addr":"台湾"}
			          ]
	 }
	 
	 //全取
	 for(var i=0;i<json.baobao.length;i++){
		 alert(json.baobao[i].name);
	 }
	
  </script>
</html>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>insertBefore.html</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>

  </body>
  
  <script language="JavaScript">
   /**
	 * 案例四
	 * {
	 *   "param1":[{key:value,key:value},{key:value,key:value}],
	 *   "param2":[{key:value,key:value},{key:value,key:value}],
	 *   "param3":[{key:value,key:value},{key:value,key:value}]
	 * }
	 *  
	 *  
	 */
	 var json = {
				"baobao":[
				          	{"name":"小双","age":18,"addr":"扬州"},
				          	{"name":"建宁","age":18,"addr":"北京海淀"},
				          	{"name":"龙儿","age":38,"addr":"岛国"},
				          	{"name":"阿珂","age":17,"addr":"台湾"}
				          ],
				"haohao":[
							{"name":"楠楠","age":23,"addr":"北京昌平修正"},
							{"name":"倩倩","age":18,"addr":"上海"}
				          ]
		 }
	 
	 
	 //娶楠楠
	 alert(json.haohao[0].name);

  </script>
</html>

json的转换格式

json的转换工具是通过java封装好的一些jar工具包,直接将java对象或集合转换成json格式的字符串。

4.4.2 常见的json转换工具

工具名称介绍
JsonlibJava 类库,需要导入的jar包较多
Gsongoogle提供的一个简单的json转换工具
Fastjsonalibaba技术团队提供的一个高性能的json转换工具
Jackson开源免费的json转换工具,springmvc转换默认使用jackson
使用方法

1)导入json相关jar包
2)创建java对象或集合
3) 使用jackson的ObjectMapper对象的writeValueAsString方法进行转换

package com.itheima.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonDemo {

	public static void main(String[] args) throws JsonProcessingException {
		
		//创建User对象
		User user = new User();
		user.setId("100");
		user.setUsername("haohao");
		user.setAge(33);
		//创建List集合
		List<String> arr = new ArrayList<>();
		arr.add("test1");
		arr.add("test2");
		arr.add("test3");
		//创建Map集合
		Map<String,User> map = new HashMap<>();
		map.put("user", user);
		
		//转换
		ObjectMapper om = new ObjectMapper();
		String userJson = om.writeValueAsString(user);
		String arrJson = om.writeValueAsString(arr);
		String mapJson = om.writeValueAsString(map);
		
		System.out.println(userJson);
		System.out.println(arrJson);
		System.out.println(mapJson);	
	}	
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值