可能重複:
How to parse JSON in Objective-C?Objective - C:如何閱讀json?
我目標C完全新的,我需要從谷歌距離矩陣JSON距離的值,但數據我可以從JSON只是第一個獲得元素, 所以如何獲得距離這個json的價值?
{
"destination_addresses": [
"San Francisco, Californie, États-Unis",
"Victoria, BC, Canada"
],
"origin_addresses": [
"Vancouver, BC, Canada",
"Seattle, État de Washington, États-Unis"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "1 732 km",
"value": 1732128
},
"duration": {
"text": "3 jours 23 heures",
"value": 340902
},
"status": "OK"
}
]
}
],
"status": "OK"
}
確定這裏是我的示例代碼,剛剛從JSON教程如下:
我已經使用SBJSON,但我只能行關鍵,所以如何讓距離鍵值?
SBJSON *parser = [[SBJSON alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
URLWithString:@"http://maps.googleapis.com/maps/api/distancematrix/json?origins=Vancouver+BC|Seattle&destinations=San+Francisco|Victoria+BC&mode=bicycling&language=fr-FR&sensor=false"]];
// Perform request and get JSON back as a NSData object
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
NSArray *statuses = [parser objectWithString:json_string error:nil];
for (NSDictionary *status in statuses)
{
NSLog(@"%@ - %@", [status objectForKey:@"rows"]);
}
你到目前爲止嘗試過哪些代碼? –
請再檢查一次!我發佈了一些示例代碼! – user999218