我是新程序员。我正在使用Google Matrix API。我收到以下响应,我想获取“文本”:“ 1686公里”。我正在使用Json解析。谢谢

"destination_addresses" : [ "San Francisco, Californie, États-Unis" ],

   "origin_addresses" : [ "Vancouver, BC, Canada" ],

   "rows" : [

      {

         "elements" : [

            {

               "distance" : {

                  "text" : "1 686 km",

                  "value" : 1685690

               },

               "duration" : {

                  "text" : "3 jours 21 heures",

                  "value" : 336418

               },

               "status" : "OK"

            }
         ]
      }
   ],
   "status" : "OK"
}



  SBJsonParser *json = [[SBJsonParser new] autorelease];

  NSError *jsonError;

  parsedJSON = [json objectWithString:data error:&jsonError];

最佳答案

好吧,parsedJSON将是一个NSDictionary,因此:

NSArray *rows = [parsedJSON objectForKey:@"rows"];
for (NSDictionary *row in rows) {
    NSArray *elements = [row objectForKey:@"elements"];
    for (NSDictionary *element in elements) {
        NSDictionary *distance = [element objectForKey:@"distance"];
        NSString *kmDistance = [distance objectForKey:@"text"]; ///< That's what you wanted
    }
}

关于iphone - JSON解析。值不在公里中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9681913/

10-12 14:42