2013-01-07 222 views
0

我的response給了我null值,但是當我在fiddler中運行相同時,我得到大量數據。我寫的代碼非常少,並使用在線工具來生成我的代碼。我想知道我在搞什麼。JSON在響應中返回空值

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); 
} 
} 

我在assertTrue(response.getPublicationInfo().size() > 0);得到一個測試用例失敗。但是小提琴手返回以下

{ 
"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" 
     } 
    ] 
} 
} 
+1

我建議你先確定你是否真的得到了響應 - 在JSON解析之前。 –

+0

SpotlightPublicationInfo!= PublicationInfo?你在哪裏映射後者? – Perception

回答

5

你應該在所有地方改變@JsonProperty("PublicationInfo")@JsonProperty("SpotlightPublicationInfo")。你的屬性名稱錯誤。

@JsonProperty的語義是:它出現在json中的屬性名稱。

編輯也從json中去除虛假的屬性名稱。替換:

response = mapper.readValue(connection.getInputStream(), Publications.class); 

有了:

JsonNode responseJson = mapper.readTree(connection.getInputStream()); 
response = mapper.readValue(responseJson.traverse().getValueAsString("SpecialPublications"), Publications.class); 

這將去掉虛假{"SpecialPublications": ... }包裝您嘗試解析的對象。

+0

我做了這個改變,仍然有同樣的問題。 junit測試未通過,提琴手返回整車數據。 – taxeeta

+1

@taxeeta:是的,還有一個原因。如果json沒有'{「SpecialPublications」:...}'包裝器,你的代碼將會工作。讓我想想什麼是最簡單的方法來刪除它。如果你總是通過正則表達式去除它,它會是一個選項嗎? –

+1

@taxeeta這裏是我的建議。如果我犯了錯誤,請回復,因爲我只在文本編輯器中寫過它,並沒有自己嘗試過。 –