SBJSON parsing issue with Twitter's GET trends

2019-08-25 03:17发布

问题:

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

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];

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.

回答1:

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

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];

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];
}