ios - 获取没有括号的值

标签 ios objective-c json

NSString *urlPath=[NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false",coordinate1.latitude,coordinate1.longitude,coordinate2.latitude,coordinate2.longitude];

//NSLog(@"the url for searching is : %@",urlPath);
NSURL *url=[NSURL URLWithString:urlPath];
NSData *data=[NSData dataWithContentsOfURL:url];
NSDictionary *object = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSNumber *results = [[[[object valueForKey:@"routes"]valueForKey:@"legs"]valueForKey:@"distance"]valueForKey:@"value"];

NSLog(@"Count %@",results);

结果如下:

(
    (
        12078
    )
)

能不能把号码拿出来?

最佳答案

查看 Google API 的 map 我可以看出,路线包含路段,路段具有距离属性。

{
  "status": "OK",
  "routes": [ {
    "summary": "I-40 W",
    "legs": [ {
      "steps": [ {
        "travel_mode": "DRIVING",
        "start_location": {
          "lat": 41.8507300,
          "lng": -87.6512600
        },
        "end_location": {
          "lat": 41.8525800,
          "lng": -87.6514100
        },
        "polyline": {
          "points": "a~l~Fjk~uOwHJy@P"
        },
        "duration": {
          "value": 19,
          "text": "1 min"
        },
        "html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
        "distance": {
          "value": 207,
          "text": "0.1 mi"
        }
      },
      ...
      ... additional steps of this leg
    ...
    ... additional legs of this route
      "duration": {
        "value": 74384,
        "text": "20 hours 40 mins"
      },
      "distance": {
        "value": 2137146,
        "text": "1,328 mi"
      },
      "start_location": {
        "lat": 35.4675602,
        "lng": -97.5164276
      },
      "end_location": {
        "lat": 34.0522342,
        "lng": -118.2436849
      },
      "start_address": "Oklahoma City, OK, USA",
      "end_address": "Los Angeles, CA, USA"
    } ],

所以第一条路线第一段的距离应该是:

NSNumber *distance = object[@"routes"][0][@"legs"][0][@"distance"][@"value"];

关于ios - 获取没有括号的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20623675/

相关文章:

ios - 太多的文字将我的按钮推到屏幕下方

iphone - 如何将正数变为负数,反之亦然?

C# RestSharp - 反序列化非标准 JSON

javascript - PrototypeJS 版本 1.6.0.2 覆盖 JSON.parse 和 JSON.stringify 并破坏 socket.io 功能

iphone - 这是从后台线程访问 UI 工具包的安全方法吗...?

javascript - 如何在 Grunt-Uncss 中包含 @media 忽略函数

ios - pdf 的操作扩展

ios - 创建一个 txt 文件 - swift : IOS

IOS facebook app 滑动选项菜单

objective-c - NSTreeController 添加 :method not adding "after the current selection"