将JSON数组解析为NSDictionary [英] Parsing a JSON array into a NSDictionary
问题描述
我正在使用Weather Underground API来创建一个应用程序,我在解析与严重警报相关的块时遇到了麻烦。 JSON使用具有子键值对的键值对 - 这对我来说并不是一个问题,因为我可以使用后续的NSDictionaries - 但严重警报的条目已经证明是有问题的。见下文:
I'm working with the Weather Underground API to make an app and I've hit a snag while parsing the block relating to severe alerts. The JSON uses key-value pairs that have sub key value pairs -- which haven't been a problem for me, as I can make subsequent NSDictionaries out of those -- but the entry for severe alerts has proven problematic. See below:
"alerts": [
{
"type": "WAT",
"description": "Flash Flood Watch",
"date": "3:13 PM EDT on April 28, 2012",
"date_epoch": "1335640380",
"expires": "8:00 AM EDT on April 29, 2012",
"expires_epoch": "1335700800",
"message": "\u000A...Flash Flood Watch in effect through Sunday morning...\u000A\u000AThe National Weather Service in Charleston has issued a\u000A\u000A* Flash Flood Watch for portions of northeast Kentucky... (Note: I trimmed this for length's sake),
"phenomena": "FF",
"significance": "A"
}
]
警报对与我能够解析的其他对不同,因为它有 []括号围绕子值,我不确定如何清除它以便我可以访问子值。在其他示例中我已经能够解析,它只有 {}括号,而不是{}和[]括号。作为参考,括号始终存在 - 即使没有恶劣天气警报......在这种情况下,警报对返回括号[],不存在子对。
The "alerts" pair differs from others I've been able to parse because it has this [ ] bracket surrounding the sub-values and I'm not sure how to clear it so I can access the subvalues. In the other examples I've been able to parse, it only has the { } brackets, and not both the { } and [ ] brackets. For reference, the brackets are always present -- even when there are no severe weather alerts... in that instance the "alerts" pair returns the brackets [ ] with no sub-pairs present.
有没有办法可以从NSDictionary中删除[]括号,或者忽略它们?任何建议将不胜感激!
Is there a way I can remove the [ ] brackets from the NSDictionary, or otherwise ignore them? Any advice would be appreciated!
有关参考和故障排除帮助,以下是我如何解析JSON文档的其余部分成功:
For reference and troubleshooting help, here's how I'm parsing the rest of the JSON document successfully:
1)从原始JSON创建NSDictionary
1) Create an NSDictionary from the raw JSON
//Process Weather Call
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
2)为嵌套的json对创建后续词典
2) Create subsequent dictionaries for nested json pairs
NSDictionary *current_observation = [json objectForKey:@"current_observation"];
3)分配值
NSString* weather;
weather = [current_observation objectForKey:@"weather"];
因此,最终结果将是一个字母表,其中显示Partly Cloudy或其他内容,以及众多相关内容天气价值,我还没有显示。这些解析成功,因为它们只有范围括号{},而不是[]括号。
So the end result would be a string that says "Partly Cloudy" or something, along with numerous related weather values that I haven't shown. These parse successfully because they only have the scope brackets { }, and not the [ ] brackets.
推荐答案
括号表示Json数据存在于数组中。您可以将其解析如下:
The brackets means the Json data there are in an array. You can parse it as following
NSArray *alertArray = [json objectForKey:@"alerts"];
现在你应该遍历所有警报并解析它们(在你的情况下它只有1,但它可以更多在另一个json字符串中):
now you should loop through all alerts and parse them (in your case it's only 1, but it could be more in another json string):
//parse each alert
for (NSDictionary *alert in alertArray ){
NSString* description = [alert objectForKey:@"description"];
//etc...
}
这篇关于将JSON数组解析为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!