RestKit如何映射数组内的数组

时间:2016-01-21 05:21:04

标签: ios objective-c restkit restkit-0.20 restkit-0.24.x

以下是回复:

{
  "status": true,
  "statuscode": 200,
  "result": [
    {
      "name": "ABC",
      "date": "2015-01-30",
      "documents": [
        {
          "id": 1,
          "name": "doc1",
          "status": "complete",
        },
        {
          "id": 2,
          "name": "doc2",
          "status": "complete",
        },
        {
          "id": 3,
          "name": "doc3",
          "status": "complete",
        }
      ],
      "message": "Hello World",
      "status": 3
    }
  ]
}

我想映射并获取所有"文档"键入的数组内部"结果"而且我不需要任何其他对象/映射。我只需要文件。如何在响应描述符中完成/声明自动将所有这些文档与我的托管对象匹配?

2 个答案:

答案 0 :(得分:0)

试试这个: -

NSDictionary *dic=@{
                    @"status": @true,
                    @"statuscode":@ 200,
                    @"result": @[
                            @{
                                @"name": @"ABC",
                                @"date": @"2015-01-30",
                                @"documents": @[
                                        @{
                                            @"id":@ 1,
                                            @"name": @"doc1",
                                            @"status": @"complete",
                                            },
                                        @{
                                            @"id":@ 2,
                                            @"name": @"doc2",
                                            @"status":@ "complete",
                                            },
                                        @{
                                            @"id":@ 3,
                                            @"name": @"doc3",
                                            @"status": @"complete",
                                            }
                                        ],
                                @"message": @"Hello World",
                                @"status":@ 3
                                }
                            ]
                    };

我将您的回复存储在NSDictionary中并通过 -

获取
 [[[dic objectForKey:@"result"] objectAtIndex:0] objectForKey:@"documents"]

根据需要更改indexNumber!

答案 1 :(得分:0)

NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];  //From Server you will get response data in form of NSData , so the 'responseData' is a type of NSData
NSArray *appDetais = [jsonData objectForKey:@"result"];
NSDictionary *resultJsonData = [appDetais objectAtIndex:0];
NSArray *documentDetailsArray = [jsonData resultJsonData:@"documents"];
for(int i=0;i<[documentDetailsArray count];i++){
   NSDictionary *singleDocumentDetail = [documentDetailsArray objectAtIndex:0];
   NSLog(@"%@",[singleDocumentDetail objectForKey:@"id"]);
}

你可以尝试这个.. :))