JSON框架解析错误
我在解析 Google 地方信息中的 JSON 时遇到问题。
我有以下代码用于解析它:
NSString *myRawJson = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-15.815347,-47.9164097&radius=500&types=bank&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];
if ([myRawJson length] == 0) {
[myRawJson release];
return;
}
SBJSON *parser = [[SBJSON alloc] init];
list = [[parser objectWithString:myRawJson error:nil] copy];
NSDictionary *results = [myRawJson JSONValue];
placesLatitudes = [[NSMutableArray alloc] init];
NSDictionary *latslngs = [[results objectForKey:@"results"] valueForKey:@"location"];
NSArray *teste = [latslngs objectForKey:@"location"];
for (NSDictionary *element in teste)
{
NSString *latitude = [element objectForKey:@"lat"];
NSLog(@"%@", latitude);
}
[parser release];
URL 请求返回以下 JSON:
{
"html_attributions" : [ "Listagens por \u003ca href=\"http://www.telelistas.net/\"\u003eTelelistas\u003c/a\u003e" ],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -15.8192680,
"lng" : -47.9146680
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "1a01f2887f1a70bd430d6a7ffa2a7e46974a1cb7",
"name" : "Banco Bradesco S/A",
"reference" : "CnRqAAAAXr_6FCDMlpeVF-E0b3cDxNFGzmS1bYBBGc4v4lKrcusGQPEnx1MXnCJVb3nCVWalu2IOwN9oSVtcXS6_W8JLL_CMhKzkm75UqGt5ShX_s0d4coxOBYsbo66JP1NpF9c5Ua7OxyjepQferD6SbAIjIhIQZ6qcgQT8hqAXtFTuPzuZThoUhrCcxKMRwZq2vl8Sv8LJes7d-no",
"types" : [ "bank", "finance", "establishment" ],
"vicinity" : "CRS 511 Bl B s/n lj 15/21 - Brasília"
},
{
"geometry" : {
"location" : {
"lat" : -15.8177890,
"lng" : -47.9126370
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "e2c56c8ca6643e0f0cef6bd0405fd8c6a30850ad",
"name" : "UNIBANCO-União de Bancos Brasileiros S/A",
"reference" : "CpQBgQAAABkp74S1WF0LfZEhkNj6TUbmiPu2djL81IDnFMJhRR2HDx7336PlRh46q16FwCao290T1smo1wNsGQ-sRVZ_S-MClYUDQzdpaTdNVty0JHBjQTEOMVo0yW8Uzd_OcuI12v8eZ81wu5V7sgHomBw-SeE-mhrPntOU1EzmOANNhIDRExdKmrof2hIKHdLJJaccdRIQeQ9uN-L66Ztmz4_2dkk7DhoUZxcYaZqXgXXnAwWB97e6bNCNp8A",
"types" : [ "bank", "finance", "establishment" ],
"vicinity" : "Bl CRS 510 Bl A s/n - Brasília"
}
],
"status" : "OK"
}
我正在尝试获取路径结果中的信息 >几何形状>地点>但是我使用上面的代码得到了错误: -[__NSArrayI objectForKey:]: 无法识别的选择器发送到实例 0x5a526e0。
有人知道如何使用 iOS 的 JSON 框架解析这些数据吗? 谢谢!
I'm having a problem on parsing a JSON from Google Places.
I have the following code for parsing it:
NSString *myRawJson = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://maps.googleapis.com/maps/api/place/search/json?location=-15.815347,-47.9164097&radius=500&types=bank&sensor=true&key=AIzaSyBLY-lBALViJ6ybrgtOqQGhsCDQtsdKsnc"]];
if ([myRawJson length] == 0) {
[myRawJson release];
return;
}
SBJSON *parser = [[SBJSON alloc] init];
list = [[parser objectWithString:myRawJson error:nil] copy];
NSDictionary *results = [myRawJson JSONValue];
placesLatitudes = [[NSMutableArray alloc] init];
NSDictionary *latslngs = [[results objectForKey:@"results"] valueForKey:@"location"];
NSArray *teste = [latslngs objectForKey:@"location"];
for (NSDictionary *element in teste)
{
NSString *latitude = [element objectForKey:@"lat"];
NSLog(@"%@", latitude);
}
[parser release];
The URL request returns the following JSON:
{
"html_attributions" : [ "Listagens por \u003ca href=\"http://www.telelistas.net/\"\u003eTelelistas\u003c/a\u003e" ],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -15.8192680,
"lng" : -47.9146680
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "1a01f2887f1a70bd430d6a7ffa2a7e46974a1cb7",
"name" : "Banco Bradesco S/A",
"reference" : "CnRqAAAAXr_6FCDMlpeVF-E0b3cDxNFGzmS1bYBBGc4v4lKrcusGQPEnx1MXnCJVb3nCVWalu2IOwN9oSVtcXS6_W8JLL_CMhKzkm75UqGt5ShX_s0d4coxOBYsbo66JP1NpF9c5Ua7OxyjepQferD6SbAIjIhIQZ6qcgQT8hqAXtFTuPzuZThoUhrCcxKMRwZq2vl8Sv8LJes7d-no",
"types" : [ "bank", "finance", "establishment" ],
"vicinity" : "CRS 511 Bl B s/n lj 15/21 - Brasília"
},
{
"geometry" : {
"location" : {
"lat" : -15.8177890,
"lng" : -47.9126370
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "e2c56c8ca6643e0f0cef6bd0405fd8c6a30850ad",
"name" : "UNIBANCO-União de Bancos Brasileiros S/A",
"reference" : "CpQBgQAAABkp74S1WF0LfZEhkNj6TUbmiPu2djL81IDnFMJhRR2HDx7336PlRh46q16FwCao290T1smo1wNsGQ-sRVZ_S-MClYUDQzdpaTdNVty0JHBjQTEOMVo0yW8Uzd_OcuI12v8eZ81wu5V7sgHomBw-SeE-mhrPntOU1EzmOANNhIDRExdKmrof2hIKHdLJJaccdRIQeQ9uN-L66Ztmz4_2dkk7DhoUZxcYaZqXgXXnAwWB97e6bNCNp8A",
"types" : [ "bank", "finance", "establishment" ],
"vicinity" : "Bl CRS 510 Bl A s/n - Brasília"
}
],
"status" : "OK"
}
I'm trying to get the information in the path results > geometry > location > lat but I with the code above I get the error: -[__NSArrayI objectForKey:]: unrecognized selector sent to instance 0x5a526e0.
Someone know how to parse this data using JSON Framework for iOS?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
results 是一个数组,而不是字典。 location 是字典,而不是数组。
我没有测试编译该代码,但它应该接近。
results is an array, not a dictionary. location is a dictionary, not an array.
I did not test compile that code, but it should be close.