在iPhone上使用JSON框架-帮助! [英] Using JSON Framework on iPhone - HELP!

查看:100
本文介绍了在iPhone上使用JSON框架-帮助!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用以下代码来解析发送的JSON链接.这也是我也为即将发布的我的iPhone应用程序向Google Reader API发送GET调用的方式.

Currently I am using the following code to parse the JSON link sent. This is how I also send a GET call to the Google Reader API for an upcoming iPhone application of mine.

- (NSArray *)subscriptionList
{
if(!cookies && [cookies count] == 0) {
    [self requestSession];
}

NSString * url = @"http://www.google.com/reader/api/0/subscription/list?output=json&client=scroll";

ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url]];
[request setRequestMethod:@"GET"];
[request setRequestCookies:cookies];
[request addRequestHeader:@"Authorization" value:[NSString stringWithFormat:@"GoogleLogin auth=%@", [self auth]]];

[request startSynchronous];

subfeeds = [NSMutableArray array];

// Create new SBJSON parser object
SBJSON *parser = [[SBJSON alloc] init];

if ([request responseStatusCode] == 200) {

    NSData * sixty = [request responseData];

    NSString * body = [[NSString alloc] initWithData:sixty encoding:NSUTF8StringEncoding];
    if (body) {
        NSArray *feeds = [parser objectWithString:body error:nil];
        NSLog(@"Array Contents: %@", [feeds valueForKey:@"subscriptions"]);
        NSLog(@"Array Count: %d", [feeds count]);

        NSDictionary *results = [body JSONValue];
        NSArray *ohhai = [results valueForKey:@"subscriptions"];

        for (NSDictionary *title in ohhai) {
            subTitles = [title objectForKey:@"title"];
            NSLog(@"title is: %@",subTitles);
        }
    }
}

return subfeeds;
[subTitles release];
[parser release];
}

我可以使用上面的代码成功解析JSON,然后将标题成功输出到NSLog.在我的RootViewController.m中,我调用以下命令来获取此-(NSArray *)subscriptionList.

I can successfully parse the JSON using the above code, and it successfully outputs the titles into NSLog. In my RootViewController.m, I call the following to grab this -(NSArray *)subscriptionList.

-(void)viewDidAppear:animated {
GoogleReader * reader = [[GoogleReader alloc] init];
[reader setEmail:gUserString];
[reader setPassword:gPassString];

//feedItems is a NSArray where we store the subscriptionList NSArray
feedItems = [reader subscriptionList];

//NSString *feedTitle = [];

NSLog(@"%@", feedItems);

[reader release];
// the rest of the function
}

上面的代码可成功使用输入的凭据.如您所见,还有一个注释为NSTi的feedTitle.这是我要从解析的JSON中提取@"title"的地方,但我不知道如何调用它.

The code above successfully works with the credentials entered. As you can see there is also a commented NSString called feedTitle. This is where I want to pull the @"title" from the parsed JSON but I do not know how to call it.

任何帮助将不胜感激!

这是JSON源的样子:

This is what the JSON source looks like:

{"subscriptions":
[
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""},
{"id":"","title":"","categories":[],"sortid":"","firstitemmsec":""}
]
}

我只对"title"节点感兴趣.

I'm interested in only the "title" node.

推荐答案

好吧,如果您添加了源JSON会有所帮助,但是很容易掌握SBJSON如何解析传入的JSON.

Well, it would help if you added the source JSON but it's quite easy to grasp how SBJSON parses incoming JSON.

只是一个示例:

{ "myOutDict" : { "key1": "val1" , "key2" : "val2"} }

此JSON字符串将被解析,因此您可以使用此代码访问它

This JSON String would be parsed so you can access it by using this code

NSDictionary* myOuterdict = [feeds valueForKey:@"myOutDict"]);
NSString* val1 =  [myOuterdict valueForKey:@"key1"]);
NSString* val2 =  [myOuterdict valueForKey:@"key2"]);

修改:检查了我的个人Google阅读器供稿:

Edit: Checked my personal Google Reader feed:

JSON看起来像这样

The JSON looks like this

{
    "subscriptions": [{
        "id": "feed/http://adambosworth.net/feed/",
        "title": "Adam Bosworth's Weblog",
        "categories": [],
        "sortid": "0B5B845E",
        "firstitemmsec": "1243627042599"
    },
    {
        "id": "feed/http://feeds.feedburner.com/zukunftia2",
        "title": "Zukunftia",
        "categories": [],
        "sortid": "FCABF5D4",
        "firstitemmsec": "1266748722471"
    }]
}

因此,相应的目标C代码为:

So the corresponding Objective C Code would be:

NSArray* subscriptions= [feeds valueForKey:@"subscriptions"]);
foreach(NSDictionary* item in subscriptions) {
    // Do stuff 
    // NSString* title = [item valueForKey:@"title"]
    // NSString* id = [item valueForKey:@"id"]
}

这篇关于在iPhone上使用JSON框架-帮助!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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