java对象 到 json字符串
Model model = new Model();
model.setAge(25);
List<Model> models = new ArrayList<Model>();
models.add(models);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(model);
mapper.writeValueAsString(models);
java对象 到 jsonNode、objectNode、arrayNode对象
jsonNode <- json string <- java object
json字符串 到 java对象
String json = "{\"age\" : 25}";
ObjectMapper mapper = new ObjectMapper();
Model model = mapper.readValue(json, Model.class);
String json2 = "[{\"age\" : 25}]";
Model models = mapper.readValue(json2, new TypeReference<List<Model>>);
json字符串 到 jsonNode、objectNode、arrayNode对象
String json = "{\"age\" : 25}";
String json2 = "[{\"age\" : 25}]";
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(json);
ObjectNode objectNode = (ObjectNode)jsonNode;
JsonNode jsonNode = mapper.readTree(json2);
ArrayNode arrayNode = (ArrayNode)jsonNode; //if is array object
jsonNode等对象 到 json字符串
ObjectMapper mapper = new ObjectMapper();
mapper.writeValueAsString(jsonNode);
mapper.writeValueAsString(arrayNode);
jsonNode等对象 到 java对象
jsonNode -> json string -> java object
class Model {
private int age;
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
}