Twitter的GET Trends/:woeid的SBJSON解析问题 [英] SBJSON parsing issue with Twitter's GET trends/:woeid

查看:163
本文介绍了Twitter的GET Trends/:woeid的SBJSON解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试理解使用sbjson解析通过调用Twitter的GET Trends/:woeid返回的以下json时遇到的问题

I am trying to understand an issue I am having when using sbjson to parse the following json returned by a call to Twitter's GET trends/:woeid

我正在使用以下URL:@"http://api.twitter.com/1/trends/1.json",我得到以下响应:(为了节省空间而被截断了)

I am using the following URL: @"http://api.twitter.com/1/trends/1.json" and I get the following response: (truncated to save space)

[  
  {  
    "trends": [  
      {  
        "name": "Premios Juventud",  
        "url": "http://search.twitter.com/search?q=Premios+Juventud",  
        "query": "Premios+Juventud"  
      },  
      {  
        "name": "#agoodrelationship",  
        "url": "http://search.twitter.com/search?q=%23agoodrelationship",  
        "query": "%23agoodrelationship"  
      }  
    ],  
    "as_of": "2010-07-15T22:40:45Z",  
    "locations": [  
      {  
        "name": "Worldwide",  
        "woeid": 1  
      }  
    ]  
  }  
]  

这是我用来解析并显示名称和网址的代码:

Here is the code I'm using to parse and display the name and url:

NSMutableString *content = [[NSMutableString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];

[content replaceCharactersInRange:NSMakeRange(0, 1) withString:@""];
[content replaceCharactersInRange:NSMakeRange([content length]-1, 1) withString:@""];
NSLog(@"Content is: %@", content);

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *json = [parser objectWithString:content];


//NSArray *trends = [json objectForKey:@"trends"];
NSArray *trends = [json objectForKey:@"trends"];
for (NSDictionary *trend in trends)
{
    [viewController.names addObject:[trend objectForKey:@"name"]];
    [viewController.urls addObject:[trend objectForKey:@"url"]];
}

[parser release];

这是示例代码,已被破坏,因为它针对的是Twitter的GET Trends(现已弃用).该代码仅在我手动删除第一个'['和最后一个']'时有效.但是,如果我不从响应中删除这些字符,则解析器将返回一个NSString元素,其中一个 NSString元素:json响应.

This is sample code that is broken because it was targeted to Twitter's GET trends call which is now deprecated. The code will only work if I manually remove the first '[' and last ']'. However if I don't remove those characters from the response, the parser returns a NSArray of one NSString element: the json response.

我应该如何正确解析此响应.预先感谢.

How should I correctly parse this response. Thanks in advance.

推荐答案

我自己解决了这个问题,我对NSArray感到困惑,因为只有一个元素似乎是字符串.

I worked the issue out myself, I was confused by the NSArray coming back with only one element that appeared to be a string.

数组中的一个元素不是NSString而是NSDictionary,一旦我理解了这一点,我就可以通过将元素分配给NSDictionary来正确处理数据,然后使用适当的键访问趋势"数据:

The one element in the array was not an NSString but a NSDictionary, once I understood this I could approach the data correctly by assigning the element to a NSDictionary, then accessing the "trends' data with the appropriate key:

NSMutableString *content = [[NSMutableString alloc] initWithBytes:[responseData bytes] length:[responseData length] encoding:NSUTF8StringEncoding];

SBJsonParser *parser = [[SBJsonParser alloc] init];
NSArray *json = [parser objectWithString:content];

NSDictionary *trends = [json objectAtIndex:0];
for (NSDictionary *trend in [trends objectForKey:@"trends"])
{
    [viewController.names addObject:[trend objectForKey:@"name"]];
    [viewController.urls addObject:[trend objectForKey:@"url"]];
}

[parser release];

使用Apple提供的最新发布的NSJSONSerialization可以使其更加干净:

It's a bit cleaner using the newly released NSJSONSerialization provided by Apple:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{ 
    NSArray *json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];    

    NSDictionary *trends = [json objectAtIndex:0];
    for (NSDictionary *trend in [trends objectForKey:@"trends"])
    {
        [viewController.names addObject:[trend objectForKey:@"name"]];
        [viewController.urls addObject:[trend objectForKey:@"url"]];
    }    

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [viewController.serviceView reloadData];
}

这篇关于Twitter的GET Trends/:woeid的SBJSON解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆