0

私は私に値responseを与えていますが、同じことを で実行すると、多くのデータが得られます。私は非常に少ないコードを記述し、オンライン ツールを使用してコードを生成しました。どこをいじっているのか気になります。nullfiddler

    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"
        }
   ]
}
}
4

1 に答える 1

5

すべての場所で変更する必要があり@JsonProperty("PublicationInfo")ます。@JsonProperty("SpotlightPublicationInfo")属性名が間違っています。

のセマンティクスは@JsonProperty、json に表示される属性名です。

EDITまた、json から偽の属性名を取り除きます。交換:

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

と:

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

{"SpecialPublications": ... }これにより、解析しようとするオブジェクトの偽のラッパーが取り除かれます。

于 2013-01-07T13:14:00.633 に答える