Objective c 将JSON数组解析到NSDictionary中
我正在使用Weather Underground API制作一个应用程序,在解析与严重警报相关的块时遇到了一个障碍。JSON使用具有子键值对的键值对——这对我来说不是问题,因为我可以用这些键值对生成后续的NSDictionary——但是严重警报的条目被证明是有问题的。见下文:Objective c 将JSON数组解析到NSDictionary中,objective-c,json,ios5,nsdictionary,Objective C,Json,Ios5,Nsdictionary,我正在使用Weather Underground API制作一个应用程序,在解析与严重警报相关的块时遇到了一个障碍。JSON使用具有子键值对的键值对——这对我来说不是问题,因为我可以用这些键值对生成后续的NSDictionary——但是严重警报的条目被证明是有问题的。见下文: "alerts": [ { "type": "WAT", "description": "Flash Flood Watch", "date": "3:13 PM EDT on April
"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"
}
]
“警报”对与我能够分析的其他对不同,因为它在子值周围有一个[]括号,我不知道如何清除它以便访问子值。在我能够解析的其他示例中,它只有{}括号,而没有{}和[]括号。作为参考,括号始终存在——即使没有恶劣天气警报。。。在这种情况下,“警报”对返回没有子对的方括号[]
有没有办法从NSDictionary中删除[]方括号,或者忽略它们?任何建议都将不胜感激
以下是我如何成功解析JSON文档的其余部分,以供参考和疑难解答帮助: 1) 从原始JSON创建NSDictionary
//Process Weather Call
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
2) 为嵌套的json对创建后续字典
NSDictionary *current_observation = [json objectForKey:@"current_observation"];
3) 赋值
NSString* weather;
weather = [current_observation objectForKey:@"weather"];
因此,最终结果将是一个字符串,上面写着“部分多云”或其他什么,以及许多我没有显示的相关天气值。这些解析成功,因为它们只有作用域括号{},而没有[]括号。括号表示数组中的Json数据。您可以如下解析它
NSArray *alertArray = [json objectForKey:@"alerts"];
现在,您应该遍历所有警报并解析它们(在您的示例中,它只有1,但在另一个json字符串中可能更多):
好的,我让它工作了——我想在这里提供一个例子,因为我最终不得不基于@Lefteris给出的建议来让它工作
我最终不得不首先将json数组作为NSArray传递,然后使用数组的第一个元素将其转换为NSDictionary。之后的一切都如@Lefteris所描述的那样起作用
所以,最后,我得到的是:
NSArray *alerts = [json objectForKey:@"alerts"];
NSDictionary *alertDict = [[NSDictionary alloc] init];
//Check that no alerts exist to prevent crashing
if([alerts count] < 1) {
NSLog(@"No Alerts Here!");
type = nil;
...
}
else //Populate fields
{
alertDict = [alerts objectAtIndex:0];
for (NSDictionary *alert in alertDict)
{
NSLog(@"Printing alert!");
type = [alertDict objectForKey:@"type"];
...
}
}
NSArray*警报=[json objectForKey:@“警报”];
NSDictionary*alertDict=[[NSDictionary alloc]init];
//检查是否不存在任何警报以防止崩溃
如果([警报计数]<1){
NSLog(@“此处无警报!”);
类型=零;
...
}
else//填充字段
{
alertDict=[alerts objectAtIndex:0];
用于(alertDict中的NSDictionary*警报)
{
NSLog(@“打印警报!”);
类型=[alertDict objectForKey:@“类型”];
...
}
}
这让我开始使用单个数组迭代运行,我希望我可以简单地迭代数组,因为我知道计数并处理任何其他警报。再次感谢你的帮助 当然,一个数组!那些括号应该把它泄露出去。。。我想我今天工作太久了p无论如何,这在警报上下文中也是有意义的——在给定的时间内可能有多个警报,因此应该有某种方法在API中处理。我试试看——谢谢你的帮助!这段代码现在仍然有效,我用你的例子试过了itself@Khay很抱歉当时对我来说很好。在我看来,2年过去了,却没有人在1万次浏览中指出问题,这似乎令人怀疑。您是否使用与我相同的iOS版本和相同(或类似)的数据?另外,回顾一下Lefteris答案的编辑历史——去年那里的代码发生了变化,所以新版本很可能已经发生了变化。我真的不能告诉你,因为我不再使用iOS了。
NSArray *alerts = [json objectForKey:@"alerts"];
NSDictionary *alertDict = [[NSDictionary alloc] init];
//Check that no alerts exist to prevent crashing
if([alerts count] < 1) {
NSLog(@"No Alerts Here!");
type = nil;
...
}
else //Populate fields
{
alertDict = [alerts objectAtIndex:0];
for (NSDictionary *alert in alertDict)
{
NSLog(@"Printing alert!");
type = [alertDict objectForKey:@"type"];
...
}
}