欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

JSON 及 jackson工具

程序员文章站 2022-05-28 22:30:36
...

JSON

JSON简介

概述:json是一种轻量级的数据交互格式、它是基于ECMAScript语法实现一个全新的语言。由于格式比较简单和清晰非常有利于人和阅读和编写、以及的解析和传输、所以说广泛的应用于数据交互。
校验json是否正常的地址.

JSON格式:

数组格式

[1, “pogne”, true]

对象格式

{
“uid”: 1,
“name”: “李小路”,
“sex”: “女”,
“hobby”: [“吃”, “喝”, “嫖”, “赌”]
}

对象数组

[{
“uid”: 1,
“name”: “李小路”,
“sex”: “女”,
“hobby”: [“吃”, “喝”, “嫖”, “赌”]
},
{
“uid”: 2,
“name”: “pgone”,
“sex”: “男”,
“hobby”: [“吃”, “喝”, “嫖”, “赌”]
},
{
“uid”: 3,
“name”: “马苏”,
“sex”: “女”,
“hobby”: [“嫖”]
}
]

复杂格式

{
“classId”: “IT001”,
“stus”: [{
“id”: 1,
“name”: “李小璐”,
“sex”: “女”,
“hobby”: [“吃”, “喝”, “嫖”, “赌”]
},
{
“id”: 2,
“name”: “pgone”,
“sex”: “男”,
“hobby”: [“吃”, “喝”, “嫖”, “赌”]
},
{
“id”: 1,
“name”: “马苏”,
“sex”: “女”,
“hobby”: [“嫖”]
}
]
}

json格式的语法规范:
  1、用[]表示一个数组 用{}表示一个对象
  2、数组中可以写0-N个元素、每个元素用,隔开最后一个不加,每个元素的类型是object任意类型
  3、对象中可以有0-N个属性、属性和属性值呈键值对形式、其中键必须是字符串、值可以是object任意类型,每个属性之间用,隔开最后一个属性不加 ,

JSON应用

解析口诀
  遇见数组就遍历、遇见对象.属性

js解析json数组

@WebServlet("/array")
public class ArrayServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//解决响应乱码
		resp.setContentType("text/html;charset=utf-8");
		//响应一个json数组
		resp.getWriter().print("[1, true, \"哈哈\"]");
	}

}
	<script type="text/javascript">
			
		$.ajax({
			url:"${pageContext.request.contextPath}/array",
			type:"get",
			dataType:"json",
			success:function(d){
				$.each(d,function(index,dd){
					console.log(index+"\t"+dd)
				})
			}
			
		})			
	</script>

js解析json对象

@WebServlet("/object")
public class ObjectServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//解决响应乱码
		resp.setContentType("text/html;charset=utf-8");
		//响应一个json对象
		resp.getWriter().print("{\r\n" + 
				"	\"id\": 1,\r\n" + 
				"	\"name\": \"pgone\",\r\n" + 
				"	\"sex\": \"男\",\r\n" + 
				"	\"hobby\": [\"吃\", \"嫖\"]\r\n" + 
				"\r\n" + 
				"}");
	}

}
	<script type="text/javascript">
			
		$.get("${pageContext.request.contextPath }/object",{},function(d){
			console.log(d.id+"\t"+d.name+"\t"+d.sex+"\t")
			//遍历的爱好     hb:表示从爱好数组中获取的每一个爱好
			$.each(d.hobby,function(index,hb){
				console.log(hb)
			})
		},"json")
	
	</script>

js解析json 对象数组

@WebServlet("/obj")
public class ObjectArrayServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//解决响应乱码
		resp.setContentType("text/html;charset=utf-8");
		//响应一个json对象
		resp.getWriter().print("[{\r\n" + 
				"		\"id\": 1,\r\n" + 
				"		\"name\": \"pgone\",\r\n" + 
				"		\"sex\": \"男\",\r\n" + 
				"		\"hobby\": [\"吃\", \"嫖\"]\r\n" + 
				"\r\n" + 
				"	},\r\n" + 
				"	{\r\n" + 
				"		\"id\": 2,\r\n" + 
				"		\"name\": \"李小璐\",\r\n" + 
				"		\"sex\": \"女\",\r\n" + 
				"		\"hobby\": [\"嫖\"]\r\n" + 
				"\r\n" + 
				"	},\r\n" + 
				"	{\r\n" + 
				"		\"id\": 3,\r\n" + 
				"		\"name\": \"马苏\",\r\n" + 
				"		\"sex\": \"女\",\r\n" + 
				"		\"hobby\": [\"嫖\"]\r\n" + 
				"\r\n" + 
				"	}\r\n" + 
				"]");
	}

}

	<script type="text/javascript">
			
		$.post("${pageContext.request.contextPath }/obj",{},function(d){
			
			$.each(d,function(index,student){
				console.log(student.id+"\t"+student.name+"\t"+student.sex+"\t")
				
				$.each(student.hobby,function(ind,hb){
					console.log(hb)
				})
				
			})
			
		},"json")
	</script>
	

js解析复杂格式

@WebServlet("/fz")
public class FzServlet extends HttpServlet {

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//解决响应乱码
		resp.setContentType("text/html;charset=utf-8");
		//响应一个json对象
		resp.getWriter().print("{\r\n" + 
				"	\"ClassID\": \"IT001\",\r\n" + 
				"	\"stus\": [{\r\n" + 
				"			\"id\": 1,\r\n" + 
				"			\"name\": \"pgone\",\r\n" + 
				"			\"sex\": \"男\",\r\n" + 
				"			\"hobby\": [\"吃\", \"嫖\"]\r\n" + 
				"\r\n" + 
				"		},\r\n" + 
				"		{\r\n" + 
				"			\"id\": 2,\r\n" + 
				"			\"name\": \"李小璐\",\r\n" + 
				"			\"sex\": \"女\",\r\n" + 
				"			\"hobby\": [\"嫖\"]\r\n" + 
				"\r\n" + 
				"		},\r\n" + 
				"		{\r\n" + 
				"			\"id\": 3,\r\n" + 
				"			\"name\": \"马苏\",\r\n" + 
				"			\"sex\": \"女\",\r\n" + 
				"			\"hobby\": [\"嫖\"]\r\n" + 
				"\r\n" + 
				"		}\r\n" + 
				"	]\r\n" + 
				"\r\n" + 
				"}");
	}

}



	<script type="text/javascript">
		
		$.ajax({
			url:"${pageContext.request.contextPath }/fz",
			dataType:"json",
			type:"post",
			success:function(d){
				console.log(d.ClassID)
				$.each(d.stus,function(index,student){
					console.log(student.id+"\t"+student.name+"\t"+student.sex)
					$.each(student.hobby,function(ind,hb){
						console.log(hb)
					})
				})
			}
		})
	</script>

Jackson工具

使用步骤
 1、导入jackson包
JSON 及 jackson工具JSON 及 jackson工具JSON 及 jackson工具
 2、创建工具转换对象
   new ObjectMapper()
 3、调用转换方法
  writeValueAsString(java对象)
代码示例

/*
	 * java数组转json数组
	 */
	@Test
	public void arrayToJson() throws JsonProcessingException {
		Object arr[] = {1,true,"哈哈"};
	    //创建工具转换对象
	    ObjectMapper mapper = new  ObjectMapper();
	    //调用转换方法
		String asString = mapper.writeValueAsString(arr);
		System.out.println(asString);
	}
	/*
	 * java对象 转json对象
	 */
	@Test
	public void objectToJson() throws JsonProcessingException {
		
		Student student = new Student(1,"马保国","男","五连鞭、健身、吹牛逼","大师","小伙子你不讲武德我告诉你",new Date());
	    //创建工具转换对象
	    ObjectMapper mapper = new  ObjectMapper();
	    //调用转换方法
		String asString = mapper.writeValueAsString(student);
		
		System.out.println(asString);
	}
	/*
	 * java集合 转json对象数组
	 */
	@Test
	public void objectArrayToJson() throws JsonProcessingException {
		Student stu1= new Student(1,"马保国","男","五连鞭、健身、吹牛逼","大师","小伙子你不讲武德我告诉你",new Date());
		Student stu2 = new Student(2,"切克哇啦","男","偷电瓶、进*","王者","打工、这辈子是不能打工的",new Date());
		
		ArrayList<Student> stu_list = new ArrayList<Student>();
		stu_list.add(stu1);
		stu_list.add(stu2);
		
		 //创建工具转换对象
	    ObjectMapper mapper = new  ObjectMapper();
	    //调用转换方法
		String asString = mapper.writeValueAsString(stu_list);
		System.out.println(asString);
	}
	/*
	 * java复杂格式转换为json对象
	 */
	@Test
	public void fzToJson() throws JsonProcessingException {
		ClassRoom room = new ClassRoom();
		room.setClassId("IT001");
		Student stu1= new Student(1,"马保国","男","五连鞭、健身、吹牛逼","大师","小伙子你不讲武德我告诉你",new Date());
		Student stu2 = new Student(2,"切克哇啦","男","偷电瓶、进*","王者","打工、这辈子是不能打工的",new Date());
		ArrayList<Student> stu_list = new ArrayList<Student>();
		stu_list.add(stu1);
		stu_list.add(stu2);
		room.setStus(stu_list);
		 //创建工具转换对象
	    ObjectMapper mapper = new  ObjectMapper();
	    //调用转换方法
		String asString = mapper.writeValueAsString(room);
		System.out.println(asString);
	}