public class People implements Serializable{
private Long id;
private String name;
private Integer age;
public People() {
}
public People(Long id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
public class A<E,F>{
private E e;
private F f;
public F getF() {
return f;
}
public void setF(F f) {
this.f = f;
}
public E getE() {
return e;
}
public void setE(E e) {
this.e = e;
}
}
public class JsonTest {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper(); // create once, reuse
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true)
.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
TypeFactory typeFactory = mapper.getTypeFactory();
//json to pojo
String jsonSource = "{id:1,name:'liyuan',age:20}";
People people = mapper.readValue(jsonSource, People.class); //recommend
people = mapper.readValue(jsonSource,new TypeReference<People>(){});
people = mapper.readValue(jsonSource, typeFactory.constructType((People.class)));
//json to map
Map<String,Object> map = mapper.readValue(jsonSource,new TypeReference<HashMap<String,Object>>(){}); //recommend
System.out.println(mapper.writeValueAsString(map));
map = mapper.readValue(jsonSource, typeFactory.constructMapType(HashMap.class, String.class, Object.class));
System.out.println(mapper.writeValueAsString(map));
//json to node
JsonNode root = mapper.readTree(jsonSource);
System.out.println(root.get("id").asLong());
System.out.println(root.get("name").asText());
//json to array
jsonSource = "[{id:1,name:'liyuan',age:20},{id:2,name:'xiaoming',age:15}]";
People[] arr = mapper.readValue(jsonSource, new TypeReference<People[]>(){});
System.out.println(mapper.writeValueAsString(arr));
arr = mapper.readValue(jsonSource, typeFactory.constructArrayType(People.class));
System.out.println(mapper.writeValueAsString(arr));
//json to list
List<People> list = mapper.readValue(jsonSource, new TypeReference<List<People>>(){});
System.out.println(mapper.writeValueAsString(list));
list = mapper.readValue(jsonSource, typeFactory.constructCollectionType(ArrayList.class,People.class));
System.out.println(mapper.writeValueAsString(list));
//json to generic
jsonSource = "{e:{id:1,name:'liyuan',age:20},f:{id:2,name:'xiaoming',age:15}}";
A<People,People> generic = mapper.readValue(jsonSource, new TypeReference<A<People,People>>(){});
System.out.println(mapper.writeValueAsString(generic));
generic = mapper.readValue(jsonSource, typeFactory.constructParametricType(A.class,People.class,People.class));
System.out.println(mapper.writeValueAsString(generic));
}
}
<audio controls="controls" style="display: none;"></audio>