【问题标题】:How to parse an array containing the trending topics from twitter json feed如何解析包含来自 twitter json feed 的热门话题的数组
【发布时间】:2013-08-06 13:41:45
【问题描述】:

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

这是 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);

}

有什么想法吗?

【问题讨论】:

    标签: objective-c json twitter


    【解决方案1】:

    你不能像这样直接使用jsondata,你需要导航到趋势数组:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-08
      • 2015-09-10
      • 2010-11-15
      • 2013-05-24
      • 2017-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-05-11
      相关资源
      最近更新 更多