2017-09-11 185 views
1

下面是我的示例JSON數據。它在轉換過程中給出以下例外。還有另外一類有交易清單。JSON轉換問題

交易

public class Transactions{ 
     private String id; 
     private ThisAccount thisAccount; 
     private OtherAccount otherAccount; 
     private Details details; 
     private Metadata metadata; 

     public String getId(){ 
     return id; 
     } 
     public void setId(String input){ 
     this.id = input; 
     } 
     public ThisAccount getThisAccount(){ 
     return thisAccount; 
     } 
     public void setThisAccount(ThisAccount input){ 
     this.thisAccount = input; 
     } 
     public OtherAccount getOtherAccount(){ 
     return otherAccount; 
     } 
     public void setOtherAccount(OtherAccount input){ 
     this.otherAccount = input; 
     } 
     public Details getDetails(){ 
     return details; 
     } 
     public void setDetails(Details input){ 
     this.details = input; 
     } 
     public Metadata getMetadata(){ 
     return metadata; 
     } 
     public void setMetadata(Metadata input){ 
     this.metadata = input; 
     } 
     public String toString() { 
      return this.getId(); 
     } 
} 

JSON

{ 
    "transactions":[ 
     { 
     "id":"dcb8138c-eb88-404a-981d-d4edff1086a6", 
     "this_account":{ 
      "id":"savings-kids-john", 
      "holders":[ 
       { 
        "name":"Savings - Kids John", 
        "is_alias":false 
       } 
      ], 
      "number":"832425-00304050", 
      "kind":"savings", 
      "IBAN":null, 
      "swift_bic":null, 
      "bank":{ 
       "national_identifier":"rbs", 
       "name":"The Royal Bank of Scotland" 
      } 
     }, 
     "other_account":{ 
      "id":"c83f9a12-171e-4602-9a92-ae895c41b16b", 
      "holder":{ 
       "name":"ALIAS_CBCDE5", 
       "is_alias":true 
      }, 
      "number":"13677980653", 
      "kind":"CURRENT PLUS", 
      "IBAN":"BA12 1234 5123 4513 6779 8065 377", 
      "swift_bic":null, 
      "bank":{ 
       "national_identifier":null, 
       "name":"The Bank of X" 
      }, 
      "metadata":{ 
       "public_alias":null, 
       "private_alias":null, 
       "more_info":null, 
       "URL":null, 
       "image_URL":null, 
       "open_corporates_URL":null, 
       "corporate_location":null, 
       "physical_location":null 
      } 
     }, 
     "details":{ 
      "type":"sandbox-payment", 
      "description":"Description abc", 
      "posted":"2016-10-09T20:01:53Z", 
      "completed":"2016-10-09T20:01:53Z", 
      "new_balance":{ 
       "currency":"GBP", 
       "amount":null 
      }, 
      "value":{ 
       "currency":"GBP", 
       "amount":"10.00" 
      } 
     }, 
     "metadata":{ } 
     }, 
     { 
     "id":"06ffa118-7892-45c7-8904-f938766680dd", 
     "this_account":{ }, 
     "other_account":{ }, 
     "details":{ }, 
     "metadata":{ } 
     }, 
     { 
     "id":"2d633a56-cd59-4ee5-8dae-142750a1a1b4", 
     "this_account":{ }, 
     "other_account":{ }, 
     "details":{ }, 
     "metadata":{ } 
     }, 
] 
} 

異常

com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "this_account" (class com.transactionRetrieval.controller.Transactions), not marked as ignorable (5 known properties: "details", "id", "otherAccount", "thisAccount", "metadata"]) 
at [Source: [email protected]; line: 1, column: 79] (through reference chain: com.transactionRetrieval.controller.TransactionsList["transactions"]->java.util.ArrayList[0]->com.transactionRetrieval.controller.Transactions["this_account"]) 

它是否有做的變量名是thisAccount代替this_account什麼。有沒有一種方法來映射這個。有工作的方式嗎?

回答

4

是否必須對變量名稱爲thisAccount而不是this_account進行任何操作。

是的,它的確如此。

有沒有辦法映射這個。

是,查找點數3

有沒有辦法,使工作?

是的,他們中的3,我現在能


你的JSON中含有一種叫this_account場,但你的類不,它包含thisAccount認爲。

有3種方式解決這個:

  1. 更改您的變量名:this_account
  2. 您的JSON字段更改爲thisAccount
  3. (推薦)使用@JsonProperty註釋上thisAccount像這個:

    @JsonProperty("this_account") 
    private ThisAccount thisAccount; 
    

    這會將您的JSON屬性this_account「鏈接」或「映射」到變量thisAccount

您需要爲JSON和類中具有不同名稱的所有其他變量/字段執行相同的操作。

參考:https://github.com/FasterXML/jackson-annotations#annotations-for-renaming-properties

1

即使是對方的回答你的問題的作品,我不認爲標註每一個屬性爲推薦的方式做到這一點。相反,使用一個命名策略:

ObjectMapper mapper = new ObjectMapper(); 
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); 
1

Frakcool的建議,讓您得到明確的鏈接JSON屬性名到Java屬性名稱,它是經常去的最安全的方式時。

但您的示例可能會更簡單一些:它看起來像命名遵循一種模式,其中JSON中的multi_word_property在Java中變爲multiWordProperty

這是一種常見的風格,傑克遜實際上已經內置了對它的支持。當你創建ObjectMapper,你可以像這樣配置它:

ObjectMapper mapper = ...; 

// jackson-databind versions ≥ 2.7 
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); 

// jackson-databind versions < 2.7 
mapper.setPropertyNamingStrategy(
    PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); 

映射器應該從後您的示例處理的結構。