2017-04-07 222 views
0

我是我在創建傑克遜映射有點失落。我的JSON有以下結構傑克遜傑森POJO映射

{ 
    "d": { 
    "__metadata": { 
     "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/JobApplication(1463L)", 
     "type": "SFOData.JobApplication" 
    }, 
    "lastName": "K", 
    "address": "123 Main Street", 
    "cellPhone": "12345", 
    "firstName": "Katrin", 
    "city": "Anytown", 
    "country": "United States", 
    "custappattachment": { 
     "results": [ 
     { 
      "__metadata": { 
      "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/Attachment(1188L)", 
      "type": "SFOData.Attachment" 
      }, 
      "fileExtension": "jpeg", 
      "fileName": "hp-hero-img.jpeg", 
      "fileContent": "/9j/4AA" 
     }, 
     { 
      "__metadata": { 
      "uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/Attachment(1189L)", 
      "type": "SFOData.Attachment" 
      }, 
      "fileExtension": "jpeg", 
      "fileName": "hp-content-bkgd-img.jpeg", 
      "fileContent": "/9j/4AAQSk" 
     }]}}} 

我發現很多教程處理陣列,但我最先令牌「d」已經失敗。 和所有「__metadata」令牌根本不需要。

我創建了一個pojo,其中包含像lastName等屬性和一個集合附件。 但我的代碼總是失敗的象徵 「d」 或 「__metadata」

public class ResponseDataObject { 


    private String lastName; 
    private String address; 
    private String cellPhone; 
    private String firstName; 
    private String city; 
    private String country; 
    private List<Attachment> attachments = new ArrayList<>(); 
    .....getters and setters 

和傑克遜讀者

ObjectReader objectReader = 
    mapper.readerFor(ResponseDataObject.class); 
    ResponseDataObject dataObject = objectReader.readValue(file); 

任何提示,將不勝感激。

問候 馬蒂亞斯

回答

0

您需要忽略的屬性,不存在POJO。坐落在DeserializationFeature以下屬性爲ObjectMapper

// for version 1.x  
mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
// for newer versions 
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false) 

反序列化代碼:

ObjectMapper mapper = new ObjectMapper(); 
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); 
mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true); 
ResponseDataObject dataObject = mapper.readValue(file, ResponseDataObject.class); 

這個註釋添加到ResponseDataObject類:

@JsonRootName(value = "d") 
class ResponseDataObject { 
+0

我試過了,但它對所有屬性的結果都是空值,所以我猜它們沒有正確映射? –

+0

你正在使用稍微不同的代碼來解析jsonvalue,我已經更新了我的代碼。試一試。 –

+0

不,一切都是空的 –

0

您可以使用@JsonIgnoreProperties註釋忽略性質不符合課程中定義的任何成員,例如:

@JsonIgnoreProperties(ignoreUnknown = true) 
public class ResponseDataObject { 

這將確保所有匹配的屬性被映射並且其他屬性被忽略。