2015-02-24 154 views
0

我已經給出JSON並且不能分析部分數據。這似乎解釋到詞典:RestKit JSON映射

{ 
    "products": [ 
     { 
      "id": 6796, 
      "title": "my title", 
      "description": "desc", 
      "code": "12345", 
      "valueType": "null", 
      "discounts": [ 
       { 
        "minPrice": null, 
        "maxPrice": null, 
        "value": 20, 
        "avail": false 
       } 
      ] 
     } 
    ] 
} 

我使用的是最新版本的RESTKit的,但我不能正確discounts下解析。

我RestKit設置:

responseMapping.addAttributeMappingsFromDictionary([ 

      "id"      : "id", 
      "code"      : "code", 
      "title"      : "title", 
      "valueType"     : "valueType", 
      "description"    : "desc", 
      "discounts.minPrice"  : "minPrice", 
      "discounts.maxPrice"  : "maxPrice", 
      "discounts.value"   : "value", 
      "discounts.avail" : "avail", 
      ]) 

但低於折扣的所有值總是返回零。我做錯了什麼?

回答

1

您不能直接使用discounts.XXX進行映射,因爲discounts是一個數組,您無法將該數組編入索引並提取一個值。

您可能需要更改源JSON以將值壓縮出字典,或者創建一個自定義對象,您可以將discounts數組中的每個項目映射到該自定義對象。

從技術上講,你可以映射整個discounts數組,這會給你一個字典數組,然後你可以在setter方法中解壓縮,但是自定義對象數組通常是更好的方法。

+0

謝謝我會嘗試並更新你 – Anton 2015-02-24 22:37:25

+0

它幫助了我。我已經將映射關係嵌套數組映射爲分離的字典。 – Anton 2015-02-25 14:03:14