【发布时间】:2012-01-26 01:33:49
【问题描述】:
我有一个关于在 iOS5 中解析 JSON 响应的问题。
目前,我正在关注this 指南,以帮助我解析从第三方映射服务返回的 JSON 响应。
一切正常,只是第三方服务器返回的 JSON 响应与指南本身显示的有所不同。
简而言之,整个 JSON 响应的整体结构如下所示:
{
"directions": [....],
"messages": [....],
"routes":
{
"features": [
{
"attributes": {....},
"geometry":
{
"paths": [....]
}
}
]
}
}
This 是实际的 JSON 查询 URL。
通过使用这行代码,
NSDictionary * jsonResponse = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
我能够成功获取 jsonResponse 字典以报告它具有 3 个键/值对,但我的最终目标是检索存储在“routes.features.geometry.paths”中的数组。
这是我当前获取最后一组数组值的代码块:
NSDictionary * jsonResponse = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray * jsonArray = [jsonResponse valueForKeyPath:@"routes.features.geometry.paths"];
jsonArray = [jsonArray objectAtIndex:0];
jsonArray = [jsonArray objectAtIndex:0];
我想知道是否有人可以更好地了解我应该如何以更优雅的方式进行此操作?
提前非常感谢!
【问题讨论】: