JSON对象映射 - RestKit

时间:2015-04-09 18:31:51

标签: ios objective-c iphone json restkit

我将以下JSON作为回复。我该怎么做对象映射呢?我正在使用iOS - RestKit。

    {  
   "predictions":[  
      {  
         "description":"User1",
         "id":"75b8c57b4443f4b881f0efc94afd52437232aee9"
      },
      {  
         "description":"User2",
         "id":"82aa3303704041cda0e4fc34024d383dbf923604"
      },
      {  
         "description":"User3",
         "id":"f88f669a30934ef599bccf86c0236426cf2d313e"
      },
      {  
         "description":"User4",
         "id":"fa9ae7b65fa52bffdabf00d69e7db5cd37539781"
      },
      {  
         "description":"User5",
         "id":“66df3fd7e400eb31efd3ac935036aab2c02b03f0"
      }
   ],
   "status":"OK"
}

1 个答案:

答案 0 :(得分:1)

创建一个名为Prediction的新类,其中descriptionpredictionId为属性,然后使用映射器对象

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[Prediction class]]; [mapping addAttributeMappingsFromDictionary:@{
@"description":   @"description",
@"id":     @"predictionId",
}];

当然正确解析响应后