Java JSON在响应时返回空值
我的Java JSON在响应时返回空值,java,json,web-services,jackson,Java,Json,Web Services,Jackson,我的响应给了我null值,但是当我在fiddler中运行相同的值时,我得到了很多数据。我写的代码很少,并且使用在线工具生成代码。我不知道我把事情搞砸了 Publications response = null ; // First open URL connection (using JDK; similar with other libs) try { URL url = new URL( "http://myserver
响应
给了我null
值,但是当我在fiddler
中运行相同的值时,我得到了很多数据。我写的代码很少,并且使用在线工具生成代码。我不知道我把事情搞砸了
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) ;
而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);
}
}
您应该在所有位置将
@JsonProperty(“PublicationInfo”)
更改为@JsonProperty(“SpotlightPublicationInfo”)
。属性名称有误
@JsonProperty
的语义是:json中显示的属性名称
EDIT还可以从json中去掉虚假的属性名。替换:
response = mapper.readValue(connection.getInputStream(), Publications.class);
与:
这将去除您试图解析的对象的虚假包装。您应该在所有位置将
@JsonProperty(“PublicationInfo”)
更改为@JsonProperty(“SpotlightPublicationInfo”)
。属性名称有误
@JsonProperty
的语义是:json中显示的属性名称
EDIT还可以从json中去掉虚假的属性名。替换:
response = mapper.readValue(connection.getInputStream(), Publications.class);
与:
这将去除您试图解析的对象的虚假的
{“SpecialPublications”:…}
包装器。我建议您首先确定是否确实得到了响应——在JSON解析之前。SpotlightPublicationInfo!=公共信息?您将后者映射到哪里?我建议您首先确定是否确实得到了响应——在JSON解析之前。SpotlightPublicationInfo!=公共信息?你将后者映射到哪里?我做了这个更改,但仍然存在相同的问题。junit测试没有通过,fiddler返回一整卡车的数据。@Taxeta:是的,还有一个原因。如果json没有{“SpecialPublications”:…}
包装,则代码将正常工作。让我想想最简单的方法是什么。如果你总是通过regex删除它,这会是一个选项吗?@taxeta我的建议是这样的。如果我有任何错误,请回信,因为我只是在文本编辑器中编写的,我自己没有尝试过。我得到一个类型ObjectMapper中的readValue(JsonParser,Class)方法不适用于参数(JsonNode,Class)
eclipse错误,编译器错误。我还使用getValueAsString
重新计算了get
。我会在周一试用,然后回复。谢谢。我做了这个改变,但我还是有同样的问题。junit测试没有通过,fiddler返回一整卡车的数据。@Taxeta:是的,还有一个原因。如果json没有{“SpecialPublications”:…}
包装,则代码将正常工作。让我想想最简单的方法是什么。如果你总是通过regex删除它,这会是一个选项吗?@taxeta我的建议是这样的。如果我有任何错误,请回信,因为我只是在文本编辑器中编写的,我自己没有尝试过。我得到一个类型ObjectMapper中的readValue(JsonParser,Class)方法不适用于参数(JsonNode,Class)
eclipse错误,编译器错误。我还使用getValueAsString
重新计算了get
。我会在周一试用,然后回复。谢谢
JsonNode responseJson = mapper.readTree(connection.getInputStream());
response = mapper.readValue(responseJson.traverse().getValueAsString("SpecialPublications"), Publications.class);