【问题标题】:Using JSON Framework on iPhone - HELP!在 iPhone 上使用 JSON 框架 - 帮助!
【发布时间】:2010-10-01 03:56:02
【问题描述】:

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

- (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。

-(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
}

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

任何帮助将不胜感激!

这是 JSON 源代码的样子:

{"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”节点感兴趣。

【问题讨论】:

    标签: iphone objective-c nsarray google-reader json-framework


    【解决方案1】:

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

    只是一个示例:

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

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

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

    编辑:检查了我的个人 Google 阅读器供稿:

    JSON 看起来像这样

    {
        "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"
        }]
    }
    

    所以对应的 Objective C 代码应该是:

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

    【讨论】:

    • 好的,我可以轻松解析 JSON,但我主要需要将提要的标题实际分配给 UITableViewCell 的 textLabel.text。
    • 这很简单,有什么问题?只需使用订阅数组作为您的数据源。
    • 说实话,我不知道该怎么做。我只知道 JSON,所以我有点困惑。
    • 那是另一个问题 :) 在此处发布(但已在此处和网络上多次回答)。我回答了你的问题吗:请接受:)
    • 接受了 :) 你能把我链接到可以帮助我以适当的方式使用订阅数组作为我的数据源的东西吗?我迫切需要这项工作,只要我了解如何从适当的数组中委派这些数据源,我就可以做任何其他事情。
    【解决方案2】:

    我不确定我是否理解这个问题。您是要获取整个提要的标题还是每个项目的标题?因为我在源 JSON 中看不到订阅数组的标题属性。

    【讨论】:

      猜你喜欢
      • 2011-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-12
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多