简体   繁体   中英

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"   }
]

I 've got such a JSON and I've parse it to a NSDictionary using the following code

NSDictionary *resultDic = [NSJSerialization JSONObjectWithData:resData         options:NSJSONReadingAllowFragments error:nil];

but when I try to get the value from the keys of NSDictionary , an error occurs I found there is no key in the NSDictionary.

So how can I get the value using ObjectForKey Thanks

resultDic would be an array of dictionaries, not a dictionary itself. You want something like this:

NSArray * resultArray = [NSJSONerialization JSONObjectWithData:resData   options:NSJSONReadingAllowFragments error:nil];
NSDictionary * result = [resultArray objectAtIndex:0];
NSString * week = [result objectForKey:@"week"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM