[英]How to parse such a JSON to NSDictionary and get the keys and values
[
{
"week": "1-16",
"course": "math",
"lecturer:": "AAA",
"credit": "2" },
{
"week": "9-16",
"course": "tech",
"lecturer:": "BBB",
"credit": "2" },
{
"week": "1-8",
"course": "English",
"lecturer:": "CC",
"credit": "0.5" },
{
"week": "1-16",
"course": "German",
"lecturer:": "DDD",
"credit": "4" }
]
我有這樣的JSON,並使用以下代碼將其解析為NSDictionary
NSDictionary *resultDic = [NSJSerialization JSONObjectWithData:resData options:NSJSONReadingAllowFragments error:nil];
但是,當我嘗試從NSDictionary的鍵獲取值時,發生錯誤,我發現NSDictionary中沒有鍵。
那么如何使用ObjectForKey獲取值呢?
resultDic
將是字典的數組,而不是字典本身。 您想要這樣的東西:
NSArray * resultArray = [NSJSONerialization JSONObjectWithData:resData options:NSJSONReadingAllowFragments error:nil];
NSDictionary * result = [resultArray objectAtIndex:0];
NSString * week = [result objectForKey:@"week"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.