介绍:JSON是一种轻量级的数据交换格式,是一种特殊的字符串,方便前后端数据交互
一、JSON在JS中的使用
/**
* js中使用JSON
* 1 JSON格式是一中特殊格式的字符串,保存数据是key:value的形式
* 2 key必须使用引号包裹
* 2 value可以是普通数据类型(字符串,数字等),也可以是对象或者数组
*/
var jsonStr = '{"name":"张三","age":20,"cat":{"name":"梅里猫"},"hobby":["唱","跳"],"friend":[{"name":"李四","age":30},{"name":"王五","age":40}]}'
console.log(jsonStr);
// js中将JSON转为一个对象
var obj = JSON.parse(jsonStr)
console.log(obj);
// js中将一个对象转为JSON格式
var jsonStr02 = JSON.stringify(obj)
console.log(jsonStr02);
控制台:
二、JSON在JAVA中的使用
创建maven项目导入依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4.2</version>
</dependency>
Person类:
package com.yang;
public class Person {
private String name;
private Integer age;
private Dog dog;
public Person() {
}
public Person(String name, Integer age, Dog dog) {
this.name = name;
this.age = age;
this.dog = dog;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", dog=" + dog +
'}';
}
}
Dog类:
package com.yang;
public class Dog {
private String name;
public Dog() {
}
public Dog(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
'}';
}
}
测试类:
package com.yang;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class TestJSON {
public static void main(String[] args) throws JsonProcessingException {
// JSON在java中的使用
Person obj = new Person("jack", 20, new Dog("小狗"));
// 将对象转为JSON
ObjectMapper objectMapper = new ObjectMapper();
String jsonStr = objectMapper.writeValueAsString(obj);
System.out.println(jsonStr);
// 将JSON转为对象
// java中"需要转义
String jsonStr02 = "{\"name\":\"jack\",\"age\":20,\"dog\":{\"name\":\"小狗\"}}";
Person person = objectMapper.readValue(jsonStr02, Person.class);
System.out.println(person);
}
}
控制台:
{"name":"jack","age":20,"dog":{"name":"小狗"}}
Person{name='jack', age=20, dog=Dog{name='小狗'}}
结论:java对象转为JSON格式是对象的形式({}的形式)
三、JSON和Map集合的关系
package com.yang;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.HashMap;
public class TestMap {
public static void main(String[] args) throws JsonProcessingException {
HashMap map = new HashMap();
map.put("key1","value1");
map.put("key2",2);
map.put("key3",new Dog("小黑"));
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(map);
System.out.println(json);
}
}
控制台:
{"key1":"value1","key2":2,"key3":{"name":"小黑"}}
结论:Map集合转为JSON格式和对象转为JSOIN的格式一致({}的形式)
四、JSON和List集合以及数组的关系
package com.yang;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
public class TestListAndArr {
public static void main(String[] args) throws JsonProcessingException {
ArrayList list = new ArrayList();
list.add("jack");
list.add(2);
list.add(new Dog("小黄"));
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(list);
System.out.println(json);
String[] data = {"张三","李四","王五"};
String json02 = objectMapper.writeValueAsString(data);
System.out.println(json02);
}
}
控制台:
["jack",2,{"name":"小黄"}]
["张三","李四","王五"]
结论:List集合和数组类型转为JSON格式是数组的形式([]的形式)