使用 NSDictionary 获取特定的 Json 数据
Posted
技术标签:
【中文标题】使用 NSDictionary 获取特定的 Json 数据【英文标题】:Fetching specific Json data with NSDictionnary 【发布时间】:2014-12-08 20:55:37 【问题描述】:我想用 json 数据获取 NSDictionnary 中的特定值。 例如,我只想获取键“windspeedMiles”的第一个值(在 current_condition 中)。 我获取的 Json 数据是:
"data":
"nearest_area": [
"distance_miles": "0.0",
"latitude": "54.500",
"longitude": "-8.500"
],
"request": [
"query": "Lat 54.50 and Lon -8.50",
"type": "LatLon"
],
"weather": [
"date": "2014-12-09",
"hourly": [
"cloudcover": "59",
"humidity": "84",
"precipMM": "0.1",
"pressure": "1024",
"sigHeight_m": "3.1",
"swellDir": "290",
"swellHeight_m": "3.0",
"swellPeriod_secs": "12.6",
"tempC": "5",
"tempF": "41",
"time": "0",
"visibility": "10",
"waterTemp_C": "12",
"waterTemp_F": "53",
"weatherCode": "116",
"weatherIconUrl": [
"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0002_sunny_intervals.png"
],
"winddir16Point": "SSW",
"winddirDegree": "204",
"windspeedKmph": "34",
"windspeedMiles": "21"
,
"cloudcover": "84",
"humidity": "87",
"precipMM": "0.1",
"pressure": "1020",
"sigHeight_m": "0.6",
"swellDir": "240",
"swellHeight_m": "0.1",
"swellPeriod_secs": "12.4",
"tempC": "6",
"tempF": "42",
"time": "300",
"visibility": "10",
"waterTemp_C": "12",
"waterTemp_F": "53",
"weatherCode": "119",
"weatherIconUrl": [
"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0003_white_cloud.png"
],
"winddir16Point": "SSW",
"winddirDegree": "193",
"windspeedKmph": "45",
"windspeedMiles": "28"
,
"cloudcover": "75",
"humidity": "91",
"precipMM": "0.4",
"pressure": "1016",
"sigHeight_m": "3.1",
"swellDir": "290",
"swellHeight_m": "3.0",
"swellPeriod_secs": "11.3",
"tempC": "7",
"tempF": "44",
"time": "600",
"visibility": "2",
"waterTemp_C": "12",
"waterTemp_F": "53",
"weatherCode": "266",
"weatherIconUrl": [
"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0017_cloudy_with_light_rain.png"
],
"winddir16Point": "SSW",
"winddirDegree": "193",
"windspeedKmph": "54",
"windspeedMiles": "34"
,
"cloudcover": "0",
"humidity": "93",
"precipMM": "0.8",
"pressure": "1011",
"sigHeight_m": "1.0",
"swellDir": "250",
"swellHeight_m": "0.2",
"swellPeriod_secs": "10.6",
"tempC": "16",
"tempF": "61",
"time": "900",
"visibility": "10",
"waterTemp_C": "12",
"waterTemp_F": "53",
"weatherCode": "113",
"weatherIconUrl": [
"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0001_sunny.png"
],
"winddir16Point": "SSW",
"winddirDegree": "202",
"windspeedKmph": "62",
"windspeedMiles": "39"
,
"cloudcover": "98",
"humidity": "95",
"precipMM": "6.6",
"pressure": "1006",
"sigHeight_m": "3.7",
"swellDir": "290",
"swellHeight_m": "3.6",
"swellPeriod_secs": "11.0",
"tempC": "18",
"tempF": "65",
"time": "1200",
"visibility": "7",
"waterTemp_C": "12",
"waterTemp_F": "53",
"weatherCode": "302",
"weatherIconUrl": [
"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0018_cloudy_with_heavy_rain.png"
],
"winddir16Point": "SW",
"winddirDegree": "220",
"windspeedKmph": "64",
"windspeedMiles": "40"
,
"cloudcover": "89",
"humidity": "95",
"precipMM": "8.3",
"pressure": "1003",
"sigHeight_m": "4.3",
"swellDir": "290",
"swellHeight_m": "4.0",
"swellPeriod_secs": "10.9",
"tempC": "19",
"tempF": "65",
"time": "1500",
"visibility": "5",
"waterTemp_C": "12",
"waterTemp_F": "53",
"weatherCode": "308",
"weatherIconUrl": [
"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0018_cloudy_with_heavy_rain.png"
],
"winddir16Point": "SW",
"winddirDegree": "234",
"windspeedKmph": "60",
"windspeedMiles": "38"
,
"cloudcover": "49",
"humidity": "73",
"precipMM": "1.5",
"pressure": "1004",
"sigHeight_m": "5.7",
"swellDir": "290",
"swellHeight_m": "5.3",
"swellPeriod_secs": "10.8",
"tempC": "7",
"tempF": "45",
"time": "1800",
"visibility": "10",
"waterTemp_C": "12",
"waterTemp_F": "53",
"weatherCode": "353",
"weatherIconUrl": [
"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"
],
"winddir16Point": "W",
"winddirDegree": "261",
"windspeedKmph": "66",
"windspeedMiles": "41"
,
"cloudcover": "58",
"humidity": "73",
"precipMM": "0.5",
"pressure": "1005",
"sigHeight_m": "7.2",
"swellDir": "290",
"swellHeight_m": "6.7",
"swellPeriod_secs": "10.6",
"tempC": "5",
"tempF": "42",
"time": "2100",
"visibility": "10",
"waterTemp_C": "12",
"waterTemp_F": "53",
"weatherCode": "353",
"weatherIconUrl": [
"value": "http:\/\/cdn.worldweatheronline.net\/images\/wsymbols01_png_64\/wsymbol_0009_light_rain_showers.png"
],
"winddir16Point": "W",
"winddirDegree": "260",
"windspeedKmph": "79",
"windspeedMiles": "49"
],
"maxtempC": "19",
"mintempC": "5"
]
这是我的代码:
报告.m:
#import "Reports.h"
@interface Reports ()
@end
@implementation Reports
- (void)viewDidLoad
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://api.worldweatheronline.com/free/v2/weather.ashx?q=Bundoran&format=json&num_of_days=5&key=e1caa114b7fba6f5bcea272e1fc77"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSDictionary *dico = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSDictionary *wind = [dico objectForKey:@"windspeedMiles"];
NSLog(@"The current windspeedMiles is : %@", wind);
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
@end
【问题讨论】:
好的。你有什么问题? @Madbreaks 例如,我只想获取键“windspeedMiles”的第一个值(在 current_condition 中)。我的 NSLog 发送给我:“nil”,我希望它发送给我 current_condition 中的值,即:“19” 所以,您已经获得了 JSON 布局。使用 NSJSONSerialization 解析后,Objective-C 对象布局将是相同的。只需通过关卡访问并获取数据。 那么,dico
字典中直接包含哪些键值??
题外话,因为您要查找教程。
【参考方案1】:
要在您发布的结构中获取该特定值,您可以访问:
[[[[dico objectForKey:@"data"] objectForKey:@"current_condition"] objectAtIndex:0] objectForKey:@"windspeedMiles"]
你只需要看看你的结构。当您使用JSONObjectWithData
获取字典时,它会为每组花括号提供NSDictionary
,为每组方括号提供NSArray
。
因为您的 JSON 中的所有内容都是“数据”的子对象,我建议您将原始 NSDictionary
创建为:
NSDictionary *dico = [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil] objectForKey:@"data"];
这样您就不必一直引用“数据”字典。
【讨论】:
投反对票的原因是什么?他的答案是正确的吧?【参考方案2】:您的预期密钥位于嵌套的 NSDictionary
对象中,因此您无法使用以下方法检索它:
NSDictionary *wind = [dico objectForKey:@"windspeedMiles"];
NSLog(@"The current windspeedMiles is : %@", wind);
用途:
NSDictionary *wind = [dico[@"data"][@"condition"] objectAtIndex:0];
NSLog(@"The current windspeedMiles is : %@", wind[@"windspeedMiles"]);
【讨论】:
正是我想要的!效果很好,不要投反对票! @Viny76:我知道答案是正确的,投票者也知道答案很好。有些人,当他们获得足够的声誉时,他们会开始他们的虐待狂心态并拒绝所有人,特别是新的 SO 用户。对此不能说什么。我很高兴听到它对你有用:) 更直接(可读性更差)[[[[dico objectForKey:@"data"] objectForKey:@"current_condition"] objectAtIndex:0] objectForKey:@"windspeedMiles"] @bl4stwave 最后一个问题:如果我想访问“weatherIconUrl”中的“值”(还要输入另一个花括号和方括号),我该怎么做? @Viny76 尝试在此处复制粘贴您的 JSON 字符串:jsoneditoronline.org,以便您显示它的树形结构。不过可以直接[[[[[[dico objectForKey:@"data"] objectForKey:@"current_condition"] objectAtIndex:0] objectForKey:@"weatherIconUrl"] objectAtIndex:0] objectForKey:@"value"]
以上是关于使用 NSDictionary 获取特定的 Json 数据的主要内容,如果未能解决你的问题,请参考以下文章
使用特定键的值过滤 NSDictionary 的 NSArray
哪种检查 NSDictionary 是不是包含特定键的方法更快?