Iphone JSON解析问题

Iphone JSON解析问题,iphone,json,Iphone,Json,我的代码: NSString *jsonString; jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; // Create a dictionary from the JSON string NSDictionary *results = [jsonString JSONValue]; NSLog(@"%@",jsonString); //

我的代码:

NSString *jsonString;   

jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

// Create a dictionary from the JSON string
NSDictionary *results = [jsonString JSONValue];

NSLog(@"%@",jsonString);
// Build an array from the dictionary for easy access to each entry
NSArray *places = [results objectForKey:@"description"];
我没有得到我想要的结果。 当我调试代码时,我得到了两个密钥/值对&0对象,用于NSArray位置。

也许您应该使用:

NSArray *places = [results valueForKey:@"predictions"];

您有两个键/值对是正确的。最上面的两个键是“预测”和“状态”。因此,您必须首先提取预测(它是一个数组):

然后迭代它;还要注意,“description”是一个字符串,要获得位置,必须使用“,”分隔符拆分它:


for(NSDictionary *aPrediction in predictions) {
  NSString *description = [aPrediction objectForKey:@"description"];
  NSArray *placesInDescription = [description componentsSeparatedByString:@","];
}

适用于我,JSON工具包没有任何问题

NSString *jsonString;   

      NSData *responseData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=solapur&sensor=true&key=AIzaSyC0K5UhV_BWmXhncIZEnbh-WG2RVQVgfdY"]];

      jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

      // Create a dictionary from the JSON string
      NSDictionary *results = [jsonString objectFromJSONString];

      // Build an array from the dictionary for easy access to each entry
      NSArray *places = [results valueForKey:@"predictions"];

      NSLog(@"places %@", places);

JSON文件链接:我仍然在为NSArray predictions获取0个对象“predictions”名称中有一个输入错误,或者我没有得到您想要解释的内容。为了防止您复制并粘贴代码,引号之间有一个输入错误;如果名称拼写不正确,返回的数组将为零。@anjum请接受对您有帮助的答案-这就是StackOverflow增长的原因,人们在未来更有可能帮助您使用:NSArray*places=[results valueForKey:@“predictions”];什么是objectFromJSONString?它是用于将JSON字符串转换为对象的JSONKit方法。你使用了SBJSON吗?我导入了“JSON.h”而不是SBJSONM。由于objectFromJSONString,我的应用程序正在崩溃。我使用了JSONKit框架
NSString *jsonString;   

      NSData *responseData = [NSData dataWithContentsOfURL: [NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/autocomplete/json?input=solapur&sensor=true&key=AIzaSyC0K5UhV_BWmXhncIZEnbh-WG2RVQVgfdY"]];

      jsonString = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

      // Create a dictionary from the JSON string
      NSDictionary *results = [jsonString objectFromJSONString];

      // Build an array from the dictionary for easy access to each entry
      NSArray *places = [results valueForKey:@"predictions"];

      NSLog(@"places %@", places);