[英]JSON returning null values on response
My response
is giving me null
values, but when I'm running the same in fiddler
, I get a lot of data. 我的
response
是给我null
值,但是当我在fiddler
运行同一个值时,我得到了很多数据。 I have written very less code and used online tools to generate my code. 我编写的代码很少,并使用在线工具生成了代码。 I wonder where I am messing up.
我想知道我在哪里搞砸。
Publications response = null ;
// First open URL connection (using JDK; similar with other libs)
try {
URL url = new URL(
"http://myserver:myport/Mobile/GetPublications");
HttpURLConnection connection = (HttpURLConnection)url.openConnection() ;
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST") ;
connection.setRequestProperty("Content-Type", "application/json") ;
// and other configuration if you want, timeouts etc
// then send JSON request
Publications request = new Publications(); // POJO with getters or public fields
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(connection.getOutputStream(), request);
// and read response
response = mapper.readValue(
connection.getInputStream(), Publications.class);
} catch (JsonGenerationException e) {
e.printStackTrace();
fail() ;
} catch (JsonMappingException e) {
e.printStackTrace();
fail() ;
} catch (IOException e) {
e.printStackTrace();
fail() ;
}
assertNotNull(response) ;
assertTrue(response.getPublicationInfo() != null) ;
// assertTrue(response.getPublicationInfo().size() > 0);
// assertNotNull( ((PublicationInfo)response.getPublicationInfo().get(0)).getPublicationTitle() != null) ;
And the Publications POJO is 出版物POJO是
import com.fasterxml.jackson.*
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
@Generated("com.googlecode.jsonschema2pojo")
@JsonPropertyOrder({ "ErrorInfo", "PublicationInfo" })
public class Publications {
@JsonProperty("ErrorInfo")
private Object ErrorInfo;
@JsonProperty("PublicationInfo")
private List<PublicationInfo> PublicationInfo = new ArrayList<PublicationInfo>();
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("ErrorInfo")
public Object getErrorInfo() {
return ErrorInfo;
}
@JsonProperty("ErrorInfo")
public void setErrorInfo(Object ErrorInfo) {
this.ErrorInfo = ErrorInfo;
}
@JsonProperty("PublicationInfo")
public List<PublicationInfo> getPublicationInfo() {
return PublicationInfo;
}
@JsonProperty("PublicationInfo")
public void setPublicationInfo(
List<PublicationInfo> PublicationInfo) {
this.PublicationInfo = PublicationInfo;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperties(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
I get a test case failure at assertTrue(response.getPublicationInfo().size() > 0);
我在
assertTrue(response.getPublicationInfo().size() > 0);
处遇到测试用例失败assertTrue(response.getPublicationInfo().size() > 0);
. 。 But fiddler returns the following
但是提琴手返回以下内容
{
"SpecialPublications": {
"ErrorInfo": null,
"SpecialPublicationInfo": [
{
"Date": "2/3/2010",
"URL": "SOME URL HERE",
"Description": "So much to do so less time."
},
{
"Date": "2/4/2010",
"URL": "SOME MORE URL HERE",
"Description": "I need more time"
}
]
}
}
You should change @JsonProperty("PublicationInfo")
to @JsonProperty("SpotlightPublicationInfo")
in all places. 您应该在所有位置将
@JsonProperty("PublicationInfo")
更改为@JsonProperty("SpotlightPublicationInfo")
。 You got the attribute name wrong. 您输入的属性名称错误。
The semantics of @JsonProperty
is: the attribute name as it appears in the json. @JsonProperty
的语义是:出现在json中的属性名称。
EDIT Also strip the spurious attribute name from the json. 编辑还从json剥离虚假属性名称。 Replace:
更换:
response = mapper.readValue(connection.getInputStream(), Publications.class);
With: 附:
JsonNode responseJson = mapper.readTree(connection.getInputStream());
response = mapper.readValue(responseJson.traverse().getValueAsString("SpecialPublications"), Publications.class);
This will strip out the spurious {"SpecialPublications": ... }
wrapper of the object you try to parse. 这将
{"SpecialPublications": ... }
您尝试解析的对象的虚假{"SpecialPublications": ... }
包装器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.