`
Title
`
`
Title
`
2. $.get()
`
Title
`
3. $.post()
`
Title
`
规则:
` `
2.Json获取数据
1.Json对象.键名
2. Json对象[键名]
----------
`//定义的基本格式
var Person = {name: "zhangsan", age: "12", gender: "男"}
var name = Person.name;
var age = Person["age"];
var gender = Person['gender'];
alert(name+age+gender)`
var Persons = {
person: [
{name: "lisi", age: "113", gender: "男"},
{name: "lisi", age: "113", gender: "男"},
{name: "lisi", age: "113", gender: "男"},
{name: "lisi", age: "113", gender: "男"},
]
}
var name1 = Persons.person[1].name;
var age1 = Persons.person[1].age;
var gender1 = Persons.person[1].gender;
alert(name1+age1+gender1);
## Json的遍历 ##
//定义的基本格式
var Person = {name: "zhangsan", age: "12", gender: "男"}
//遍历Json的key和value
for(var key in Person){
//key是一个字符串, 所以不能使用Person.key
alert(key + Person[key])
}
` //嵌套格式:数组里面有多个对象
var ps = [
{name: "lisi", age: "113", gender: "男"},
{name: "lisi", age: "113", gender: "男"},
{name: "lisi", age: "113", gender: "男"},
{name: "lisi", age: "113", gender: "男"}
]
for (var i = 0; i < ps.length; i++) {
var p= ps[i]
for(var key in p){
alert(key+p[key])
}
}`
Jsonlib Gson fastJson (Alibaba) jackson(springmvc 中的解析器)
1. json转换java
@Test
public void test04() throws IOException {
//将json数据转换成java对象
//1。初始化json数据
String json="{\"gender\":\"男\",\"name\":\"zhangsan\",\"age\":12}";
//创建ObjectMapper对象
ObjectMapper mapper = new ObjectMapper();
Person person = mapper.readValue(json, Person.class);
System.out.println(person);
}
@Test
public void test05() throws IOException {//将json字符串转换为List集合
//1。初始化json数据
ArrayList list = new ArrayList<>();
list.add(new Person("zhangsan", 1212, "nan", new Date()));
list.add(new Person("zhangsan", 1212, "nan", new Date()));
list.add(new Person("zhangsan", 1212, "nan", new Date()));
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(list);
//创建ObjectMapper对象
ArrayList arrayList = mapper.readValue(s, ArrayList.class);
System.out.println(arrayList);
}
@Test
public void test07() throws IOException {
//json字符串转换成HashMap集合
Map map = new HashMap<>();
map.put("name","zhangsan");
map.put("age",12);
map.put("gender","男");
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(map);
//{"gender":"男","name":"zhangsan","age":12}
HashMap hashMap = mapper.readValue(s, HashMap.class);
Set set = hashMap.entrySet();
set.stream().forEach(name-> System.out.println(name));
}
2. java转换成json
1. 导入jar包jackson
2. 创建jackson对象ObjectMapper
3. 使用方法转换成Json字符串
# java转换Json字符串 #
` @Test
public void test1() throws IOException { //java对象转化成json字符串
Person person = new Person("zhangsan", 1212, "nan");
//创建Jackson的核心对象
ObjectMapper mapper = new ObjectMapper();
/*
* mapper.writeValue()
* 参数:
* File:将对象转换为json字符串, 并保存到指定的文件中
* Writer:将对象转换为json字符串,并将json数据填充到字符输出流中
* OutputStream:将对象转换为json对象, 并将json数据填充到字节输出流中
*
* */
String json = mapper.writeValueAsString(person);
System.out.println(json);//{"name":"zhangsan","age":1212,"gender":"nan"}
mapper.writeValue(new File("a.txt"),person);
}`
注解
1. 排除属性的 @JsonIgnore:排除属性(忽略该属性 ,在属性或者getset方法中 使用该注解)
2. @JsonFormat:格式化注解:(使用注解格式化, 用在属性或者)
@JsonFormat(pattern="yyyy-MM-DD")
@JsonIgnore
private Date birthday;**
3.list或者map类型转换成Json
@Test
public void test02() throws JsonProcessingException {
ArrayList list = new ArrayList<>();
list.add(new Person("zhangsan", 1212, "nan", new Date()));
list.add(new Person("zhangsan", 1212, "nan", new Date()));
list.add(new Person("zhangsan", 1212, "nan", new Date()));
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(list);
//[{"name":"zhangsan","age":1212,"gender":"nan"},{"name":"zhangsan","age":1212,"gender":"nan"},{"name":"zhangsan","age":1212,"gender":"nan"}]
System.out.println(s);
}
@Test
public void test03() throws JsonProcessingException {
Map map = new HashMap<>();
map.put("name","zhangsan");
map.put("age",12);
map.put("gender","男");
ObjectMapper mapper = new ObjectMapper();
String s = mapper.writeValueAsString(map);
//{"gender":"男","name":"zhangsan","age":12}
System.out.println(s);
}