0

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

这是 twitter json 提要响应的样子:

(
        {
        "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 值打印为 null。

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

}

有任何想法吗?

4

1 回答 1

0

您不能jsondata直接使用,您需要导航到趋势数组:

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

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

于 2013-08-05T18:34:35.867 回答