JSON数据处理
1.json的数据格式
1.1 数组格式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
/*
* 定义json的格式数组格式
* 中括号包裹,数组的元素的数据类型没有限制
* 元素之间,逗号分开
* */
var jsonArray = ["k1","k2",100,9.9,true];
//访问数据元素,通过索引访问
console.log(jsonArray[1]);
//遍历数组,取出数组中的元素
for(var i=0;i<jsonArray.length; i++){
console.log(jsonArray[i]);
}
</script>
</head>
<body>
</body>
</html>
1.2 对象格式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
/*
* 定义json的对象格式
* 大括号包裹,定义键值对,键必须是字符串类型,值得数据类型不限制
* 键值对之间,是冒号分开
* 每个键值对之间,逗号分开
* */
var jsonObject={
"k1":"v1",
"k2":"v2",
"k3":100,
"k4":9.9,
"k5":true
}
//取出键值对,键找值的方式
console.log(jsonObject.k1);
console.log(jsonObject.k2);
console.log(jsonObject.k3);
console.log(jsonObject.k4);
console.log(jsonObject.k5);
</script>
</head>
<body>
</body>
</html>
1.3 数据嵌套对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json数据的嵌套</title>
<script type="text/javascript">
/*
* json数据的元素是对象
* */
var jsonArray = [
{"name":"张三","age":20},
{"name":"李四","age":22}
]
//取出需要的数据 ,李四 22
console.log( jsonArray[1].name+"----"+jsonArray[1].age);
//遍历数据,取出数组中的元素
for (var i = 0; i < jsonArray.length; i++) {
console.log(jsonArray[i].name +"----"+jsonArray[i].age);
}
</script>
</head>
<body>
</body>
</html>
1.4 对象嵌套数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script type="text/javascript">
/*
* json数据是对象,对象值是数组
* */
var jsonObject = {
"k1": ["北京","上海","广州","深圳"],
"k2": ["中国","美国","英国","法国"]
}
//取出上海
console.log(jsonObject.k1[1]);
//分别取出k1和k2键的数组,遍历
for (var i = 0; i < jsonObject.k1.length; i++) {
console.log(jsonObject.k1[i]);
}
console.log("============================================")
for (var i = 0; i < jsonObject.k2.length; i++) {
console.log(jsonObject.k2[i]);
}
</script>
</head>
<body>
</body>
</html>
1.5 json嵌套
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>json数据的嵌套</title>
<script type="text/javascript">
/*
* json的数据嵌套,你中有我,我中有你
* json的数据本质是对象
* 对象是的键是字符串,对象的值是数组
* 数组的元素是对象
* */
var json = {
//键是k1,值是数组,数组的元素是对象
"k1":[
//数组的元素是两个对象
{"name":"张三","age":20},
{"name":"李四","age":21},
],
"k2":[
//数组的元素是两个对象
{"name":"王五","age":23},
{"name":"赵六","age":24},
],
}
//取出数据 李四 21
console.log(json.k1[1].name+"---"+json.k1[1].age);
//遍历k2键对应的数组
for (var i = 0; i <json.k2.length ; i++) {
console.log(json.k2[i].name+"---"+json.k2[i].age);
}
</script>
</head>
<body>
</body>
</html>
2.fastjson
它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到Java Bean。
@Data
public class Student {
private Integer id;
private String name;
private Integer age;
private String email;
private Date birthday;
}
2.1 Java对象序列化到Json
//java中的对象,Student对象,序列化Json格式字符串
@Test
public void testObjectToJson(){
Student student = new Student();
student.setId(1);
student.setName("张三");
student.setAge(20);
student.setEmail("zs@163.com");
student.setBirthday(getDate());
//student对象转成json格式的字符串
//调用静态方法,传递要抓换的对象
String jsonString = JSON.toJSONString(student);
System.out.println(jsonString);
//{"age":20,"birthday":1629451099346,"email":"zs@163.com","id":1,"name":"张三"}
}
{
"age":20,
"birthday":1629451099346,
"email":"zs@163.com",
"id":1,
"name":"张三"
}
//java中的集合List,序列化为Json格式字符串
@Test
public void testListToJson(){
//集合List,存储Student对象
ArrayList<Student> list = new ArrayList<>();
Student student1 = new Student();
student1.setId(1);
student1.setName("张三");
student1.setAge(20);
student1.setEmail("zs@163.com");
student1.setBirthday(getDate());
Student student2 = new Student();
student2.setId(2);
student2.setName("赵四");
student2.setAge(25);
student2.setEmail("zs1@163.com");
student2.setBirthday(getDate());
//student对象存储到集合中
list.add(student1);
list.add(student2);
//List集合,序列化为Json格式字符串
String jsonString = JSON.toJSONString(list);
System.out.println(jsonString);
//转后的结果是数组,数组的元素是对象
//[{"age":20,"birthday":1629451016542,"email":"zs@163.com","id":1,"name":"张三"},
// {"age":25,"birthday":1629451016542,"email":"zs1@163.com","id":2,"name":"赵四"}]
}
[
{"age":20,"birthday":1629451016542,"email":"zs@163.com","id":1,"name":"张三"},
{"age":25,"birthday":1629451016542,"email":"zs1@163.com","id":2,"name":"赵四"}
]
//java中的集合Map,序列化为Json格式字符串
@Test
public void testMapToJson(){
//创建map集合,键为字符串类型,值为Student对象
HashMap<String, Student> map = new HashMap<>();
Student student1 = new Student();
student1.setId(1);
student1.setName("张三");
student1.setAge(20);
student1.setEmail("zs@163.com");
student1.setBirthday(getDate());
Student student2 = new Student();
student2.setId(2);
student2.setName("赵四");
student2.setAge(25);
student2.setEmail("zs1@163.com");
student2.setBirthday(getDate());
//Map集合存储Student对象
map.put("student1",student1);
map.put("student2",student2);
String jsonString = JSON.toJSONString(map);
System.out.println(jsonString);
//json格式字符串是对象,对象中有两个键,键对应的值是Student对象
//{"student2":{"age":25,"birthday":1629451782886,"email":"zs1@163.com","id":2,"name":"赵四"},
// "student1":{"age":20,"birthday":1629451782886,"email":"zs@163.com","id":1,"name":"张三"}}
}
{
"student2":{"age":25,"birthday":1629451782886,"email":"zs1@163.com","id":2,"name":"赵四"},
"student1":{"age":20,"birthday":1629451782886,"email":"zs@163.com","id":1,"name":"张三"}
}
2.2 Json反序列化到java对象
//Json格式字符串,反序列化到Java对象
@Test
public void testJsonToObject(){
String jsonString = "{\"age\":20,\"birthday\":1629451099346,\"email\":\"zs@163.com\",\"id\":1,\"name\":\"张三\"}";
//Json类的静态方法 parseObject
//传递要反序列化的Json字符串,传递Java对象的class对象
Student student=JSON.parseObject(jsonString,Student.class);
System.out.println(student);
}
//Json格式字符串,反序列化到List集合
@Test
public void testJsonToList(){
String jsonString = "[{\"age\":20,\"birthday\":1629451016542,\"email\":\"zs@163.com\",\"id\":1,\"name\":\"张三\"},\n" +
" {\"age\":25,\"birthday\":1629451016542,\"email\":\"zs1@163.com\",\"id\":2,\"name\":\"赵四\"}]";
//Json类的静态方法 parseArray
//传递Json格式的字符串,传递转换后的集合的泛型的class对象
List<Student> list = JSON.parseArray(jsonString, Student.class);
list.forEach(System.out::println);
}
//Json格式字符串,反序列化到Map集合
@Test
public void testJsonToMap(){
String jsonString = "{\"student2\":{\"age\":25,\"birthday\":1629455067665,\"email\":\"zs1@163.com\",\"id\":2,\"name\":\"赵四\"}," +
"\"student1\":{\"age\":20,\"birthday\":1629455067665,\"email\":\"zs@163.com\",\"id\":1,\"name\":\"张三\"}}";
//json类的静态方法,parseObject
//直接进行反序列化,Map集合是没有泛型的,泛型没有事不安全的
//转后的集合,必须有泛型
//调用parseObject,传递参数,TypeReference类型
//将Map中所有的键存入到set集合中。因为set具备迭代器。所有可以迭代方式取出所有的键,再根据get方法。获取每一个键对应的值。
//keySet():迭代后只能通过get()取key
Map<String, Student> map = JSON.parseObject(jsonString, new TypeReference<Map<String, Student>>() {});
for (String key : map.keySet()){
System.out.println(key+":"+map.get(key));
}
}