读取 JSON 数据的 NSDictionary

Reading NSDictionary of JSON data

我有JSON个数据:

{"data":[
     {"userID":"1", "username":"name1"},
     {"userID":"2", "username":"name2"},
     {"userID":"3", "username":"name3"}
]}

返回到 NSDictionary。

NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:jsonReturnData options:kNilOptions error:&err];

jsonObject 如下所示:

然后如何读取 NSDictionary 中的值?我有:

NSDictionary *dictItem1 = (NSDictionary *)jsonObject;

读取数据项但如何获取值?

终于将数据读入 NSArray:

NSArray *JsonData = [NSJSONSerialization JSONObjectWithData:jsonReturnData options:NSJSONReadingAllowFragments error:&err];
NSArray *array = [JsonData objectAtIndex:0];
NSDictionary *dictItem = (NSDictionary *)array;
NSArray *arrayItems = [dictItem objectForKey:@"data"];

然后 arrayItems 保存了每条记录。

正如所指出的那样,进一步简化为:

NSArray *JsonData = [NSJSONSerialization JSONObjectWithData:jsonReturnData options:NSJSONReadingAllowFragments error:&err];
NSDictionary *dictItem = (NSDictionary *)[JsonData objectAtIndex:0];;
NSArray *arrayItems = [dictItem objectForKey:@"data"];