复习xml的作用:
- 作为配置文件
- 存储数据
- 作为数据传输的格式
{"key":value, "key":value, ....}
[v1, v2, v3,...]
json对象.key
json数组[索引]
let obj = {"name":"三丰", "age":100};
let v = obj.name;
alert(v);
let girls = [
{"name":"柳喲", "age":18, "addr":"北京"},
{"name":"热巴", "age":19, "addr":"新疆"},
{"name":"圆圆", "age":30, "addr":"天津"}
];
let v = girls[2].name;
alert(v);
let wulin = {
"first":"黑马",
"wudang":[
{"first" : "张" , "last" : "三丰"},
{"first" : "张" , "last" : "翠山"}
],
"emei":[
{"first" : "周" , "last" : "芷若"},
{"first" : "丁" , "last" : "敏君"}
]
}
A: 解析获取黑马:wulin.first
B: 解析获取芷若:wulin.emei.last
错误。正确的应该是:wulin.emei[0].last
C: 解析获取三丰:wulin.wudang[0].last
D: 解析获取三丰:wulin.wudang[1].last
A: {"first":"黑马", "last":"白马"}
B: {"first":"黑马", "last":[ {"first":"白", "last":"马"}] }
C: {{"first":"黑", "last":"马"}, {"first":"白", "last":"马"}}
D: [{"first":"张", "last":"三丰"}, {"first":"张", "last":"翠山"}]
客户端和服务端之间使用Ajax交互时,服务端使用JavaBean存储数据,客户端使用json存储数据。这就需要我们掌握JavaBean对象和json格式之间的转换:
常用的转换工具有:
我们要求掌握:jackson
把以下三个包,导入到web项目里:web\WEB-INF\lib
,之后 Add as Library
Jackson工具包提供了一个核心类:ObjectMapper
,这个对象提供了JavaBean和json之间转换的常用方法:
方法 | 说明 |
---|---|
ObjectMapper() |
无参构造方法 |
writeValueAsString(Object obj) |
把JavaBean对象转换成json格式的字符串 |
readValue(String json, Class clazz) |
把json格式字符串,还原成指定类型的JavaBean对象 |
readValue(InputStream is, Class clazz) |
从输入流中读取json数据,还原成指定类型的JavaBean |
package com.jackson;
public class User {
private String name;
private String sex;
private Integer age;
public User() {
}
public User(String name, String sex, Integer age) {
this.name = name;
this.sex = sex;
this.age = age;
}
//get/set 和 toString方法 省略....
}
package com.jackson;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DemoJackson {
/**
* Java转换成json
*/
@Test
public void test1() throws JsonProcessingException {
User user1 = new User("tom", "男", 20);
User user2 = new User("jerry", "女", 19);
List<User> userList = new ArrayList<>();
userList.add(user1);
userList.add(user2);
Map<String, User> userMap = new HashMap<>();
userMap.put("u1", user1);
userMap.put("u2", user2);
//1. 把一个User对象的数据,转换成json格式的字符串
// {"name":"tom","sex":"男","age":20}
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(user1);
System.out.println(json);
//2. 把一个List数据,转换成json格式的字符串
// [{"name":"tom","sex":"男","age":20},{"name":"jerry","sex":"女","age":19}]
json = mapper.writeValueAsString(userList);
System.out.println(json);
//3. 把一个Map数据,转换成json格式的字符串
// {"u1":{"name":"tom","sex":"男","age":20},"u2":{"name":"jerry","sex":"女","age":19}}
json = mapper.writeValueAsString(userMap);
System.out.println(json);
}
/**
* 把json字符串还原成Java对象
*/
@Test
public void test2() throws IOException {
String json = "{\"name\":\"tom\",\"sex\":\"男\",\"age\":20}";
//json对象形式 还原成JavaBean
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);
//json对象形式 还原成Map
Map map = mapper.readValue(json, Map.class);
System.out.println(map.get("name"));
//复杂json数组格式,还原成List(默认不带泛型是List,不能还原成List)
json = "[{\"name\":\"tom\",\"sex\":\"男\",\"age\":20},{\"name\":\"jerry\",\"sex\":\"女\",\"age\":19}]";
List list = mapper.readValue(json, List.class);
System.out.println(list.get(0));
}
}
{"key":value, "key":value, }
[v1, v2, v3...]
json对象.key
i
的值:json数组[i]
ObjectMapper mapper = new ObjectMapper();
//1. 把Java对象转换成json
String json = mapper.writeValueAsString(Object obj);
//2. 把json还原成Java对象
mapper.readValue(String json, Class clazz);