以前にも似たような質問をしたことがあるのですが、どうか、よろしくお願い致します。
(ご参考)以前の質問
【Objective-C】 GoogleのAPIで取得したJSON形式のダブルクォーテーションのついた文字列データを数値に変換する方法を教えてください
https://teratail.com/questions/44297
今回は、
緯度、経度から距離や所要時間がわかる Google Maps Directions API でJSON形式の文字列データを取得したのですが、カッコがついているため、そのままでは数値変換ができません。
このデータを数値に変換する方法を教えてください。
環境など
Objective-C
Xcode 7.3
API: Google Maps Directions API
#J-SONファイル
JSON
1 "routes" : [ 2 { 3 "bounds" : { 4 "northeast" : { 5 "lat" : 34.6935191, 6 "lng" : 132.542824 7 }, 8 "southwest" : { 9 "lat" : 34.3933369, 10 "lng" : 132.0254409 11 } 12 }, 13 "copyrights" : "地図データ ©2016 ZENRIN", 14 "legs" : [ 15 { 16 "distance" : { 17 "text" : "108 km", 18 "value" : 107975 19 }, 20 "duration" : { 21 "text" : "1時間49分", 22 "value" : 6524 23 }, 24以下略
#ソースコード
ObjectiveC
1NSString *url = [NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=34.1234,132.1234&destination=34.193753825992,132.0335519313812&sensor=false&key=****************"]; 2 3 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 4 NSData *json = [NSURLConnection sendSynchronousRequest:request6 returningResponse:nil error:nil]; 5 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingAllowFragments error:nil]; 6 7 8 NSArray *distancelist1 = [[[dic valueForKey:@"routes"] valueForKey:@"legs"] valueForKey:@"distance"]; 9 NSLog (@"distancelist1 : %@", distancelist1); 10 11 NSArray *distance1= [distancelist1 valueForKey:@"value"] ; 12 NSLog (@"distance1 : %@", distance1[0]); 13
#NSLogの結果
distancelist : ( ( { text = "20.8 km"; value = 20813; } ) ) distance1 : ( 20813 ) //このように距離(20813)の前後にカッコがつきます
以上、よろしくおねがいいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/23 12:45