問題描述
我正在使用 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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!