2013-08-05 40 views
0

我已经设置了从twitter获取指定位置的趋势主题的方法。 我的问题是,如何从JSON解析这些数据,以便我可以打印趋势主题列表的名称和URL。如何从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值打印空。

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

} 

有什么想法吗?

回答

0

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

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

然后你可以遍历所有的趋势,并提取每个名称和网址。