JSON:
1. 概念
- 概念: JavaScript Object Notation JavaScript对象表示法
Person p = new Person();
p.setName("张三");
p.setAge(23);
p.setGender("男");
var p = {"name":"张三","age":23,"gender":"男"};
- json现在多用于存储和交换文本信息的语法
- 进行数据的传输
- JSON 比 XML 更小、更快,更易解析。
2. 语法:
1. 基本规则
- 数据在名称/值对中:json数据是由键值对构成的
- 键用引号(单双都行)引起来,也可以不使用引号
- 值得取值类型:
- 数字(整数或浮点数)
- 字符串(在双引号中)
- 逻辑值(true 或 false)
- 数组(在方括号中){“persons”:[{},{}]}
- 对象(在花括号中) {“address”:{“province”:“陕西”…}}
- null
- 数据由逗号分隔:多个键值对由逗号分隔
- 花括号保存对象:使用{}定义json 格式
- 方括号保存数组:[]
2. 获取数据:
- json对象.键名
- json对象[“键名”]
- 数组对象[索引]
- 遍历
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
//1,json对象
var person={name:"zhangsan",age:23,sex:"nan"};
//1. json对象.键名
//2. json对象["键名"]
// alert(person.name);
// alert(person["name"]);
//遍历 for...in...
for(var key in person){
//alert(key);key是一个字符串,相当于person."name"
//alert(key+":"+person.key);
//alert(key+":"+person[key]);//相当于person["name"];
}
//2,第二种{}里面嵌套中括号
var personList={"persons":
[
{name:"zhangsan",age:23,sex:"男"},
{name:"zhangsan",age:23,sex:"男"},
{name:"zhangsan",age:23,sex:"男"}
]
};
//遍历
for(var key in personList){
var arr = personList[key];
for (var i = 0; i < arr.length; i++) {
var arrElement = arr[i];
for(var a in arrElement){
var value = arrElement[a];
document.write(a+":"+value);
}
document.write("<br\>");
}
}
// var name = personList.persons[0].name;
// alert(name);
//3,第三种[]里面嵌套大括号
var personArr=[
{name:"zhangsan",age:23,sex:"男"},
{name:"zhangsan",age:23,sex:"男"},
{name:"zhangsan",age:23,sex:"男"}
];
// var age = personArr[1].age;
// alert(age);
//遍历
// for (var i = 0; i < personArr.length; i++) {
// var ele = personArr[i];
// for(var key in ele){
// var value = ele[key];
// document.write(key+":"+value);
//
// }
// document.write("<br\>");
//
// }
</script>
</head>
<body>
</body>
</html>
3. JSON数据和Java对象的相互转换
- JSON解析器:
- 常见的解析器:Jsonlib,Gson,fastjson,jackson
1. JSON转为Java对象
- 导入jackson的相关jar包
- 创建Jackson核心对象 ObjectMapper
- 调用ObjectMapper的相关方法进行转换
- readValue(json字符串数据,Class)
@Test
public void test2() throws IOException {
String json = "{\"name\":\"zhangsan\",\"age\":23,\"sex\":\"男\"}";
ObjectMapper objectMapper = new ObjectMapper();
Person person = objectMapper.readValue(json, Person.class);
System.out.println(person);
}
###2. Java对象转换JSON
-
使用步骤:
-
导入jackson的相关jar包
-
创建Jackson核心对象 ObjectMapper
-
调用ObjectMapper的相关方法进行转换
- 转换方法:
- writeValue(参数1,obj):
参数1:
File:将obj对象转换为JSON字符串,并保存到指定的文件中
Writer:将obj对象转换为JSON字符串,并将json数据填充到字符输出流中
OutputStream:将obj对象转换为JSON字符串,并将json数据填充到字节输出流中 - writeValueAsString(obj):将对象转为json字符串
package com.qf.ajax; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import domain.Person; import org.junit.Test; import java.io.File; public class JacksonTest { @Test public void test() throws Exception { Person p = new Person(); p.setName("zhangsan"); p.setAge(23); p.setSex("男"); //1,创建核心对象 ObjectMapper objectMapper = new ObjectMapper(); //2,将java对象转成json字符串 String json = objectMapper.writeValueAsString(p); System.out.println("json:"+json); //3,将json写入到文件 objectMapper.writeValue(new File("person.txt"),p); } }
- 注解:
- @JsonIgnore:排除属性。
- @JsonFormat:属性值得格式化
- @JsonFormat(pattern = “yyyy-MM-dd”)
@Test public void test3() throws Exception { Person p = new Person(); p.setName("zhangsan"); p.setAge(23); p.setSex("男"); p.setBirthday(new Date()); //1,创建核心对象 ObjectMapper objectMapper = new ObjectMapper(); //2,将java对象转成json字符串 String json = objectMapper.writeValueAsString(p); System.out.println("json:"+json); //3,将json写入到文件 objectMapper.writeValue(new File("person.txt"),p); }
- 复杂java对象转换
- List:数组
- Map:对象格式一致
@Test public void test4() throws JsonProcessingException { Person p = new Person(); p.setName("zhangsan"); p.setAge(23); p.setSex("男"); p.setBirthday(new Date()); List<Person> list = new ArrayList<>(); list.add(p); //1,创建核心对象 ObjectMapper objectMapper = new ObjectMapper(); String str = objectMapper.writeValueAsString(list); System.out.println("str:"+str); Map<String,Object> map = new HashMap<>(); map.put("name","喔喔"); map.put("age",23); map.put("gender","女"); String mapStr = objectMapper.writeValueAsString(map); System.out.println("mapStr:"+mapStr); } //str:[{"name":"zhangsan","age":23,"birthday":"2019-10-22"}] //mapStr:{"gender":"女","name":"喔喔","age":23}