项目有个向外部提供api的接口,采用的Oauth协议,部分json字段存在首字母大写的情况!
好吧,还是代码来得直接:
CallBack.java
public class CallBack implements java.io.Serializable{
private int error;// 错误号
private T results; // 返回的实体信息
@Override
public String toString() {
return "CallBack [error=" + error + ", results=" + results.toString() + "]";
}
public int getError() {
return error;
}
public void setError(int error) {
this.error = error;
}
public T getResults() {
return results;
}
public void setResults(T results) {
this.results = results;
}
}
UserInfo.java
public class UserInfo implements java.io.Serializable{
@JsonProperty("AppId")
private String AppId;
@JsonProperty("AppKey")
private String AppKey;
@Override
public String toString() {
return "UserInfo [AppId=" + AppId + ", AppKey=" + AppKey + "]";
}
public String getAppId() {
return AppId;
}
public void setAppId(String appId) {
AppId = appId;
}
public String getAppKey() {
return AppKey;
}
public void setAppKey(String appKey) {
AppKey = appKey;
}
}
@JsonProperty 用来改变某个成员属性
@JsonIgnore 用来忽略某个属性
@JsonIgnoreProperties(ignoreUnknown=true) 忽略掉Pojo中没有配置的属性
Test.java
public class Test {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"error\":0, \"results\":{\"AppId\": \"werewrfasdf\", \"AppKey\":\"wqrfdsafdzsjkfhjadh\"}}";
CallBack call = mapper.readValue(json, new TypeReference>() {});
System.out.println(call.toString());
System.out.println(call.getResults().toString());
}
}
TypeReference可以用来应对复杂的范型!
简单的介绍到这了,祝各位看官身体健康万事如意!