この投稿で質問した件の続きです。
【Objective-c】 JSON形式のデータの取得方法
https://teratail.com/questions/44248?sip=n0070000_019&uid=46695
再度、初歩的な質問だと思うのですが、いろいろググってみて1日を費やしても分からず、投稿致します。
これが分からないと先に進めず、途方に暮れてます。
どうか、よろしくお願い致します。
##概要
緯度、経度から標高がわかる
Google Maps Elevation API
でJSON形式の文字列データを取得したのですが、ダブルクォーテーションがついているため、そのままでは数値変換ができません。
このデータを数値に変換する方法を教えてください
##環境など
Objective-C
Xcode 7.3
API: Google Maps Elevation API
##J-SONファイル
JSON
1{ 2 "results" : [ 3 { 4 "elevation" : 1608.637939453125, //標高データ 5 "location" : { 6 "lat" : 39.7391536, 7 "lng" : -104.9847034 8 }, 9 "resolution" : 4.771975994110107 10 } 11 ], 12 "status" : "OK" 13}
##ソースコード
objectivec
1- (void)viewDidAppear:(BOOL)animated 2{ 3 [super viewDidAppear:animated]; 4 5 NSString *url = @"https://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034&key=*********************************”; 6 7 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]]; 8 NSData *json = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 9 NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingAllowFragments error:nil]; 10 11 NSString *str = [dic[@"results"] valueForKey:@"elevation"]; // これで、str には”1608.637939453125”というダブルクオーテーションのついたデータが取得されます。 12 13 14// ここでエラーが出て、止まります。 15str = [str stringByReplacingOccurrencesOfString:@"\"" withString:@""]; // JSONのデータのダブルクォーテーションを削除するためのコード 16 17 NSInteger elevation = [str intValue]; 18 NSLog(@"標高elevation: %ld",(long)elevation); 19} 20
##ログ(エラーメッセージなど)
2016-08-14 20:39:43.748 doko[3269:631975] -[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x13ef53610 2016-08-14 20:39:43.755 doko[3269:631975] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI stringByReplacingOccurrencesOfString:withString:]: unrecognized selector sent to instance 0x13ef53610' *** First throw call stack: (0x181d86db0 0x1813ebf80 0x181d8dc4c 0x181d8abec 0x181c88c5c 0x10001d4ac 0x186efd3d4 0x186f6ec30 0x186f6ebcc 0x1871971d0 0x1871a4a80 0x181d3cf84 0x181d3c8bc 0x181d3a820 0x181c64c50 0x18354c088 0x186f4e088 0x10001e08c 0x1818028b8) libc++abi.dylib: terminating with uncaught exception of type NSException
ダブルクォーテーションを削除するために「stringByReplacingOccurrencesOfString」を使うのに、「unrecognized selector」とエラーが出てしまいます。
以上、よろしくご教授願います。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/08/14 12:24