【问题标题】:Parsing of NSDictionary of NSArray解析 NSArray 的 NSDictionary
【发布时间】:2015-09-11 10:49:46
【问题描述】:

我正在使用 Restkit API 来解析从我的网络服务器收到的 JSON 回复。来自网络服务器的回复是这种形式.scre

JSON 回复是这样的

{"Menu Items":[
            {
                "ID":393,
                "Title":"Lentil Soup",
                "Description":"This is extremely tasty",
                "Image":"Lentil_1.png"
            },
            {   
                "ID":392,
                "Title":"Chicken Tortilla Soup",
                "Description":"Quick. Simple. Delicious.":"$20",
                "Image":"Tortilla_3.png"
            }
        ]

}

我正在使用下面的代码来获取响应,但没有得到任何响应。

NSURL *baseURL = [NSURL URLWithString:@"http://URL.com"];

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; // init by url
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON];
[RKMIMETypeSerialization registerClass:[RKNSJSONSerialization class] forMIMEType:@"text/html"];

//set up restkit
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];
//setup object mapping
//addAttributeMappingsFromArray is a shortcut method to use when the JSON and your data model share the same keys, which is “name” in your case.

RKObjectMapping* boyMapping = [RKObjectMapping mappingForClass:[ListItem class] ];
[boyMapping addAttributeMappingsFromArray:@[@"ID"]];

// register mappings with the provider using a response descriptor
RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:boyMapping
                                             method:RKRequestMethodAny
                                        pathPattern:@"/demo/restaurant/app/menu_lists.php"
                                            keyPath:@"Menu Items"
                                        statusCodes:[NSIndexSet indexSetWithIndex:200]];
[objectManager addResponseDescriptor:responseDescriptor];
    NSDictionary *parameterDict = [NSDictionary dictionaryWithObjectsAndKeys:_slugString, @"item_slug", nil];
[[RKObjectManager sharedManager] getObjectsAtPath:@"/demo/restaurant/app/menu_lists.php"
                                       parameters:parameterDict
                                          success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                                              _menuItems = mappingResult.array;
                                              NSLog(@"menuItems is %@", _menuItems);
                                          }
                                          failure:^(RKObjectRequestOperation *operation, NSError *error) {
                                              NSLog(@"What do you mean by 'there is no coffee?': %@", error);
                                          }];

每次都进入失败区块。

【问题讨论】:

  • 调试日志中的错误是什么?
  • 这是错误日志,我得到“在搜索的关键路径中找不到可映射的对象表示。”
  • 粘贴收到的实际 JSON。打开跟踪日志记录。
  • @Wain 我已经粘贴了 JSON 回复
  • 在您显示的内容中看不到问题,因此您需要包含更多的日志输出

标签: objective-c json web-services restkit


【解决方案1】:

(假设收到的响应的状态码为 200)

你创建一个对象管理器:

RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client];

然后你配置它。但是,然后您尝试将其用于:

[[RKObjectManager sharedManager] getObjectsAtPath:...

这是使用在代码中其他地方创建的不同实例,并且没有您在 objectManager 上设置的任何配置。

您需要重新组织代码以确保创建并正确使用对象管理器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多