将 JSON 数组解析为 NSDictionary

Parsing a JSON array into a NSDictionary(将 JSON 数组解析为 NSDictionary)

本文介绍了将 JSON 数组解析为 NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Wea​​ther 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...u000Au000AThe National Weather Service in Charleston has issued au000Au000A* 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"];

所以最终结果将是一个字符串,上面写着部分多云"或其他内容,以及许多我没有显示的相关天气值.这些解析成功,因为它们只有范围括号 {},而不是 [] 括号.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将 JSON 数组解析为 NSDictionary

基础教程推荐