大家好,又见面了,我是全栈君。
<span style="font-family:SimHei;font-size:18px;">{ "name":" vonzhou", "age":123, "isMan":true }</span>
<span style="font-family:SimHei;font-size:18px;">package jackson.test; import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.annotation.JsonProperty; public class ExamplePOJO { @JsonProperty( "name") private String name; @JsonProperty( "age") private Integer age; @JsonProperty( "isMan") private Boolean isMan; private Map<String,Object> additionalProperties = new HashMap<String, Object>(); @JsonProperty( "name") public String getName() { return name; } @JsonProperty( "name") public void setName(String name) { this. name = name; } @JsonProperty( "age") public Integer getAge() { return age; } @JsonProperty( "age") public void setAge(Integer age) { this. age = age; } @JsonProperty( "isMan") public Boolean getIsMan() { return isMan; } @JsonProperty( "isMan") public void setIsMan(Boolean isMan) { this. isMan = isMan; } public Map<String, Object> getAdditionalProperties() { return additionalProperties; } public void setAdditionalProperties(String name, Object obj) { this. additionalProperties.put(name, obj); } @Override public String toString() { return "ExamplePOJO{\n" + "name :'" + name + "',\n" + "age : " + age + ",\n" + "isMan :" + isMan + ",\n" + "additionalProperties : " + additionalProperties + "\n}"; } } </span>
<span style="font-family:SimHei;font-size:18px;">package jackson.test;import java.io.File;import java.io.IOException;import com.fasterxml.jackson.core.JsonParseException;import com.fasterxml.jackson.databind.JsonMappingException;import com.fasterxml.jackson.databind.ObjectMapper;public class Driver { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); try { ExamplePOJO bean = mapper.readValue(new File("test.json"), ExamplePOJO.class); System.out.println("name : " + bean.getName()); System.out.println("age : " + bean.getAge() ); System.out.println("isMan : " + bean.getIsMan() ); System.out.println("==================="); System.out.println(bean.toString()); } catch (JsonParseException e) { e.printStackTrace(); }catch (JsonMappingException e){ e.printStackTrace(); }catch (IOException e){ e.printStackTrace(); } }}</span>
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/115208.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...