在iOS应用中解析json [英] Parsing json in an iOS app

查看:150
本文介绍了在iOS应用中解析json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的休息响应

I have a Rest Response like this

{
  "data": [
  {
  "id": "adhoc_Expense_process:1:43",
  "key": "adhoc_Expense_process",
  "name": "Expense process",
  "version": 1,
  "deploymentId": "10",
  "resourceName": "org\/activiti\/examples\/adhoc\/Expense_process.bpmn20.xml",
  "diagramResourceName": "org\/activiti\/examples\/adhoc\/Expense_process.png",
  "startFormResourceKey": null,
  "graphicNotationDefined": "true"
},
{.
.
.
},
]
}

我正在处理这样的响应.

I am handling the response like this.

- (void)request:(RKRequest*)request didLoadResponse:(RKResponse*)response {  

    if ([request isGET]) {  
        // Handling GET /foo.xml  

        if ([response isOK]) {  
            // Success! Let's take a look at the data  

            NSLog(@"Retrieved XML: %@", [response bodyAsString]);  
            NSString *str = [response bodyAsString];

            NSDictionary *json = [str JSONValue];

            // Get all object
            NSArray *items = [json valueForKeyPath:@"data"];

            NSEnumerator *enumerator = [items objectEnumerator];
            NSDictionary* item;
            while (item = (NSDictionary*)[enumerator nextObject]) {
                NSLog(@"clientId = %@",  [item objectForKey:@"id"]);
                NSLog(@"clientName = %@",[item objectForKey:@"key"]);
                NSLog(@"job = %@",       [item objectForKey:@"version"]);
            }

        }  

    }

当我使用bodyAsString记录响应时,我可以在控制台上看到正确的xml响应.但是我无法解析它.我正在使用SBJSON.h或换句话说是SBJSON框架.

When i log the response with bodyAsString i can see the proper xml response on the console.But i am unable to parse it.I am using SBJSON.h or in other words the SBJSON framework.

您在解析它时是否看到任何错误.错误消息是这样的.

Do you see anything wrong in the way i am parsing it.The error messages are like this..

2011-11-29 17:09:53.601 Views[6166:fb03] -[__NSCFString JSONValue]: unrecognized selector sent to instance 0x71c9200
2011-11-29 17:09:53.602 Views[6166:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString JSONValue]: unrecognized selector sent to instance 0x71c9200'

推荐答案

我在项目的BuildPhases设置中添加了以下内容.

I added the following in BuildPhases settings of the project.

sbjson-ios在目标依赖项"中 libsbjson-ios.a在与库链接二进制文件"中.

sbjson-ios in "target dependencies" libsbjson-ios.a in "link binary with libraries".

当我添加restkit框架时,我意识到我也添加了一些此类内容. 谢谢大家,它有所帮助.

I realised i added some such ones when i added the restkit framework. Thanks guys,it helped.

阿努莎

这篇关于在iOS应用中解析json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆