[英]Parsing Json to Java Object
got this problem: Json: 得到了这个问题:Json:
{"authenticationToken":{"token":"c9XXXX1-XXXX-4XX9-XXXX-41XXXXX3XXXX"}}
Object: 宾语:
public class AuthenticationToken {
public AuthenticationToken() {
}
public AuthenticationToken(String token) {
authenticationToken = token;
}
@JsonProperty(value="token")
private String authenticationToken;
public String getAuthenticationToken() {
return authenticationToken;
}
public void setAuthenticationToken(String authenticationToken) {
this.authenticationToken = authenticationToken;
}
}
But i got aa error in logs: Could not read JSON: Unrecognized field "authenticationToken" (class de.regalfrei.android.AuthenticationToken), not marked as ignorable (one known property: "token"]) and i do not have any idea how to set the JSON properties correct for this situation. 但我在日志中出现错误:无法读取JSON:无法识别的字段“authenticationToken”(类de.regalfrei.android.AuthenticationToken),未标记为可忽略(一个已知属性:“令牌”))我不知道如何为这种情况设置正确的JSON属性。 Can someone help?
有人可以帮忙吗?
As you said i added a Wrapperclass: 正如你所说,我添加了一个Wrapperclass:
public class AuthenticationTokenWrapper {
AuthenticationToken authenticationToken;
public AuthenticationTokenWrapper(AuthenticationToken authenticationToken) {
this.authenticationToken = authenticationToken;
}
@JsonProperty(value="authenticationToken")
public AuthenticationToken getAuthenticationToken() {
return authenticationToken;
}
public void setAuthenticationToken(AuthenticationToken authenticationToken) {
this.authenticationToken = authenticationToken;
}
}
and called this function: 并调用此函数:
AuthenticationTokenWrapper tok =restTemplate.postForObject(url, requestEntity, AuthenticationTokenWrapper.class);
You are using a wrapper class which have a variable named authenticationToken
which is an object of AuthenticationToken
您正在使用包装类,该类具有名为
authenticationToken
的变量,该变量是AuthenticationToken
的对象
in order to parse your JSON correctly, create a wrapper class like this 为了正确解析您的JSON,请创建一个这样的包装器类
public class Wrapper {
private AuthenticationToken authenticationToken;
public Wrapper(AuthenticationToken authenticationToken) {
this.authenticationToken = authenticationToken;
}
public AuthenticationToken getAuthenticationToken() {
return authenticationToken;
}
public void setAuthenticationToken(AuthenticationToken authenticationToken) {
this.authenticationToken = authenticationToken;
}
}
I am not sure about this... 我不确定这个......
Maybe the error is here private String authenticationToken; 也许这里的错误是私有的String authenticationToken; You are saying authenticationToken is a string, but according to the JSON Object it is another JSON Object.
您说authenticationToken是一个字符串,但根据JSON对象,它是另一个JSON对象。 Try converting it into JSON Object and access the token.
尝试将其转换为JSON对象并访问令牌。
try 尝试
@JsonProperty(value="authenticationToken")
private String authenticationToken;
then un-marshall that string into another class with 然后将该字符串取消编组到另一个类中
@JsonProperty(value="token")
private String tokenStr;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.