什么是JSON
JSON:JavaScript Object Notation JS对象简谱,是一种轻量级的数据交换格式。
对象格式
一个对象,由一个大括号表示, 括号中描述对象的属性和函数,通过键值对来描述对象的属性与函数 格式: 键和值之间用冒号连接,多个键值对用逗号分隔。 键值对的键使用引号包裹 键值对中的值可以使用JS中的任意一种数据类型
- 用Java描述一个人
class Person{
private String name;
private String age;
getter/setter/构造
}
Person p = new Person("小明",18);
- 用js来描述一个人
var p = new Object();
p.name = '小明';
p.age = 18;
- 用XML描述一个人
<person>
<name>小明</name>
<age>18</age>
</person>
- 用JSON描述一个人
{
"name":"小明",
"age":18
}
Java解析JSON
对象
public class Person{
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
Gson
1.引入gson.jar
2.调用Gson的toJSON(obj)
举例:
public class GsonTest {
public static void main(String[] args) {
Person p = new Person("小明",18);
String s = new Gson().toJson(p);
System.out.println(s);
}
}
结果:
{"name":"小明","age":18}
public class GsonTest {
public static void main(String[] args) {
String s = "{\"age\":18,\"name\":\"小明\"}";
Person person = new Gson().fromJson(s, Person.class);
System.out.println(person);
}
}
结果:
com.dmlll.Person@2a17b7b6
fastjson
1.引入fastjson.jar
2.使用fastjson的JSON.toJSONString(obj)
举例
public class FastTest {
public static void main(String[] args) {
Person p = new Person("小明",18);
String string = JSON.toJSONString(p);
System.out.println(string);
}
}
结果:
{}
此时打印不出json字符串,原因在于FastJSON是根据对象类的getter、setter方法获得JSON,此时应该为Person类添加getter、setter方法。
public class Person{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
继续运行FastTest,结果:
{"age":18,"name":"小明"}
public class FastTest {
public static void main(String[] args) {
String s = "{\"name\":\"小明\",\"age\":18}";
Person person = JSON.parseObject(s, Person.class);
System.out.println(person);
}
}
结果:
com.dmlll.Person@6f1fba17
Jackson
1.引入jackson-databind.jar、jackson-core.jar、jackson-annotations.jar
2.使用fastjson的JSON.toJSONString(obj)
public class JackTest {
public static void main(String[] args) throws JsonProcessingException {
Person p = new Person("小明",18);
ObjectMapper objectMapper = new ObjectMapper();
String string = objectMapper.writeValueAsString(p);
System.out.println(string);
}
}
结果:
{"name":"小明","age":18}
public class JackTest {
public static void main(String[] args) throws IOException {
String s = "{\"name\":\"小明\",\"age\":18}";
ObjectMapper objectMapper = new ObjectMapper();
Person person = objectMapper.readValue(s, Person.class);
System.out.println(person);
}
}
结果:
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.dmlll.Person` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{"name":"小明","age":18}"; line: 1, column: 2]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1452)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1028)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
at com.dmlll.JackTest.main(JackTest.java:11)
使用jackson将string转换成Person(反序列化)需要使用到无参构造
public class Person {
private String name;
private int age;
public Person() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
结果:
com.dmlll.Person@290d210d
|