如何从twitter json feed解析包含趋势主题的数组

时间:2013-08-05 18:26:48

标签: objective-c json twitter

我已经采用这种方法从twitter获取指定位置的热门话题。 我的问题是,如何从JSON中解析这些数据,以便我可以打印趋势主题列表的名称和URL。

这就是twitter json feed响应的样子:

(
        {
        "as_of" = "2013-08-05T18:16:52Z";
        "created_at" = "2013-08-05T18:06:52Z";
        locations =         (
                        {
                name = "San Francisco";
                woeid = 2487956;
            }
        );
        trends =         (
                        {
                events = "<null>";
                name = "#jaibrooksfollowingsession";
                "promoted_content" = "<null>";
                query = "%23jaibrooksfollowingsession";
                url = "http://twitter.com/search?q=%23jaibrooksfollowingsession";
            },
                        {
                events = "<null>";
                name = "#megalodon";
                "promoted_content" = "<null>";
                query = "%23megalodon";
                url = "http://twitter.com/search?q=%23megalodon";
            },
                        {
                events = "<null>";
                name = "#SharkWeek";
                "promoted_content" = "<null>";
                query = "%23SharkWeek";
                url = "http://twitter.com/search?q=%23SharkWeek";
            },
                        {
                events = "<null>";
                name = "#bartstrike";
                "promoted_content" = "<null>";
                query = "%23bartstrike";
                url = "http://twitter.com/search?q=%23bartstrike";
            },
                        {
                events = "<null>";
                name = "#mtvhottest";
                "promoted_content" = "<null>";
                query = "%23mtvhottest";
                url = "http://twitter.com/search?q=%23mtvhottest";
            },
                        {
                events = "<null>";
                name = "Justin Bieber";
                "promoted_content" = "<null>";
                query = "%22Justin+Bieber%22";
                url = "http://twitter.com/search?q=%22Justin+Bieber%22";
            },
                        {
                events = "<null>";
                name = "Nelson Cruz";
                "promoted_content" = "<null>";
                query = "%22Nelson+Cruz%22";
                url = "http://twitter.com/search?q=%22Nelson+Cruz%22";
            },
                        {
                events = "<null>";
                name = "The O.C.";
                "promoted_content" = "<null>";
                query = "%22The+O.C.%22";
                url = "http://twitter.com/search?q=%22The+O.C.%22";
            },
                        {
                events = "<null>";
                name = Biogenesis;
                "promoted_content" = "<null>";
                query = Biogenesis;
                url = "http://twitter.com/search?q=Biogenesis";
            },
                        {
                events = "<null>";
                name = PEDs;
                "promoted_content" = "<null>";
                query = PEDs;
                url = "http://twitter.com/search?q=PEDs";
            }
        );
    }
)

我将此响应保存到一个名为jsondata的数组中,然后使用此函数获取第一个趋势主题名称和url,但它不起作用..,名称值和url值打印为空。

- (void) decompose:(NSArray *)jsondata
{
    NSString *name = [[jsondata objectAtIndex:0]objectForKey:@"name"];
    NSString *url = [[jsondata objectAtIndex:0]objectForKey:@"url"];
    NSLog(@"name:%@",name);
    NSLog(@"url:%@",url);

}

任何想法?

1 个答案:

答案 0 :(得分:0)

你不能直接使用jsondata,你需要导航到trends数组:

NSArray *trends = [[jsondata objectAtIndex:0] objectForKey:@"trends"];

然后,您可以遍历趋势并提取每个名称和URL。