package com.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
class Person{
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class Test {
public static void main(String[] args) throws IOException {
// java.util.List l = new ArrayList();
// for (int i = 0; i < 10; i++) {
// Person p = new Person();
// p.setAge("18");
// p.setName("张三");
// l.add(p);
// }
//
// ObjectMapper objectMapper = new ObjectMapper();
// String json =objectMapper.writeValueAsString(l);
// System.out.println(json);
String json = "{\"name\":\"张三\",\"age\":\"18\"}";
ObjectMapper mapper = new ObjectMapper();
Person p = mapper.readValue(json, Person.class);
System.out.println(p.getAge());
System.out.println(p.getName());
JsonNode tree = mapper.readTree(json);
System.out.println(tree.get("name"));
}
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
class Person{
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class Test {
public static void main(String[] args) throws IOException {
// java.util.List l = new ArrayList();
// for (int i = 0; i < 10; i++) {
// Person p = new Person();
// p.setAge("18");
// p.setName("张三");
// l.add(p);
// }
//
// ObjectMapper objectMapper = new ObjectMapper();
// String json =objectMapper.writeValueAsString(l);
// System.out.println(json);
String json = "{\"name\":\"张三\",\"age\":\"18\"}";
ObjectMapper mapper = new ObjectMapper();
Person p = mapper.readValue(json, Person.class);
System.out.println(p.getAge());
System.out.println(p.getName());
JsonNode tree = mapper.readTree(json);
System.out.println(tree.get("name"));
}
}
maven 依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>