[英]How to get info from NSData?
我是iOS新手。
NSData的:
{
"results" : [
{
"formatted_address" : "Proyezd Voskresenskiye Vorota, 3, Moscow, Russia, 109012",
"geometry" : {
"location" : {
"lat" : 55.75622380,
"lng" : 37.61855850
}
我只需要“formatted_address”,你能幫我制作一個NSString,其中將是地址嗎? 抱歉愚蠢的問題。
您的NSData
是JSON響應,您需要創建一個NSDictionary
才能訪問數據的特定部分。 NSDictionary
只是將鍵映射到值。 獲取你調用的值– objectForKey:
. 在您的情況下,您有一個字典作為鍵“結果”的值。 所以在你的情況下:
NSDictionary *results = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSDictionary *resultsDictionary = [[results objectForKey:@"results"] objectAtIndex:0];
NSString *formattedAddress = [resultsDictionary objectForKey:@"formatted_address"];
看一下NSJSONSerialization類。 有一種方法可以將數據轉換為Dictionary:
+ (id)JSONObjectWithData:(NSData *)data options:(NSJSONReadingOptions)opt error:(NSError **)error;
有了它,您應該能夠通過詢問新創建的NSDictionary來檢索NSString。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.