如何在目标C中parsingJSON格式

嘿每一个我编程一个iPhone应用程序来获取谷歌search结果到我的应用程序,,,我已经使用JSON类来得到的结果…当我在JSONparsing器parsing它,并将其存储在NSDictionary我有3个键:

  1. responseData
  2. responseDetails
  3. responseStatus

重要的是第一个responseData是具有search结果…

(我认为)responseData中的另一个关键是“结果”,其中包含urls和其他东西,这是我的应用程序的最重要的部分…如何访问这个并把它放到NSDictionary中的问题… ..

这是要求:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=Paris%20Hilton 

并把事情弄清楚,请考虑把这个请求放到你的浏览器中,当你把结果复制到左边的这个网站上时,看看键盘和其他东西是什么:

 http://json.parser.online.fr/ 

日Thnx

您可以使用JSONparsing器 – SB Json将jsonstring转换为ObjectiveC对象。 请注意,ObjectiveC中有许多JSONparsing器可用,但我select了SB Json,因为它易于使用。 但根据一些基准JSONKit比SBJson更快。

一旦你有你的JSONstring使用像这样 –

 #import "JSON.h" // Create SBJSON object to parse JSON SBJSON *parser = [[SBJSON alloc] init]; // parse the JSON string into an object - assuming json_string is a NSString of JSON data NSDictionary *object = [parser objectWithString:json_string error:nil]; NSLog(@"JSON data: %@", object); 

如果您需要将Twitter的公共时间表parsing为JSON,则可以执行以下操作。同样的逻辑也可应用于您的Googlesearch结果。 你需要仔细检查你的JSON结构,所有…

 // Create new SBJSON parser object SBJSON *parser = [[SBJSON alloc] init]; // Prepare URL request to download statuses from Twitter NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json"]]; // Perform request and get JSON back as a NSData object NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // Get JSON as a NSString from NSData response NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; // parse the JSON response into an object // Here we're using NSArray since we're parsing an array of JSON status objects NSArray *statuses = [parser objectWithString:json_string error:nil]; // Each element in statuses is a single status // represented as a NSDictionary for (NSDictionary *status in statuses) { // You can retrieve individual values using objectForKey on the status NSDictionary // This will print the tweet and username to the console NSLog(@"%@ - %@", [status objectForKey:@"text"], [[status objectForKey:@"user"] objectForKey:@"screen_name"]); } 

如果我使用的语言,请使用savezdéjà注释分析器。JSON格式的代码NSDictionary,alors语言quelquesbuild议使用的语言和版本。 英国人倾向于monde。

responseData本身是一个NSDictionary,结果是一个对象。 结果碰巧是你给的情况下的一个数组。

将JSON转换为NSDictionary窗体后,您将recursion转换所有内部对象。

你可以尝试这样的事情来find你正在寻找的东西:

让我们假设完全转换的JSON在一个名为响应的NSDictionary中

 NSDictionary *responseDate = [response objectForKey:@"responseData"]; NSArray *resultsArray = [responseData objectForKey:@"results"]; 

现在你可以使用迭代器或者for循环遍历每个结果。

需要注意的一点是,如果只有一个结果,你应该首先testing一下对象的类是否是NSArray。 另外,如果没有结果,你也应该testing一下。

所以你可能想用这种方式来处理这些情况:

 NSDictionary *responseDate = [response objectForKey:@"responseData"]; If ([[responseData objectForKey:@"results"] isKindOfClass [NSArray class]]) { NSArray *resultsArray = [responseData objectForKey:@"results"]; ... do other things to get to each result in the array ... } else if ([[responseData objectForKey:@"results"] isKindOfClass [NSDictionary class]]) { // it looks like each individual result in returned in a NSDictionary in your example ... do the things to handle the single result ... } else { // handle no results returned } 

你应该做的第一件事情是,如果你不明白到底发生了什么事情,那么NSLog就是对JSONparsing器输出的description 。 这将是NSDictionary和NSArray的“嵌套”,当你看到description输出时,你会明白NSArray有一个到NSDictionary和JSON“数组”的JSON“对象”的一对一映射。 所以你“理解”parsing器输出的方式与“理解”JSON源相同。

在你的情况下,你可能会提取“responseData”对象,将其转换为NSDictionary,从中提取“结果”,将其转换为NSArray,然后遍历该数组以提取单个结果。