如何使用NSJSonSerialization从多级JSON中创建一个nsdictionary



json的示例:

注意:" docs"阵列是这个大的阵列之一阵列。

 {
        "Docs": {
            "Title1": {
                "id": "1",
                "active": "0",
                "title": "xxx",
                "date": "October 22, 2012",
                "description": "Description here",
                "ext": "DOCX",
                "when": "Tuesday, October 23",
                "subject": "French",
                "author": "xx xD",
                "email": "xx.xx@gmail.com",
                "day": "Tuesday, October 23",
                "dl": "9 downloads"
            },
            "Title2": {
                "id": "2",
                "active": "0",
                "title": "Vocab, Page 64",
                "date": "October 22, 2012",
                "description": "Description goes here",
                "ext": "XLSX",
                "when": "Tuesday October23",
                "subject": "French",
                "author": "xxxxxx",
                "email": "xxx@yahoo.com",
                "day": "Tuesday October23",
                "dl": "3 downloads"
            }
        }

例如,我如何制作一个获得"文档"数组(文档)标题的nsdictionary,也可以获取文档数组中数组数量的计数?谢谢。我对如何执行此操作非常困惑。

类似的东西:

NSError *e = nil;
NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &e];

获取了供应数据后,您可以通过字典迭代以阅读元素。要获取对象数量的计数:

// Get your object "Docs" from the root object
NSDictionary *dictionaryObject = (NSDictionary *)[JSON objectForKey:@"Docs"];
// Get the count by counting the keys
NSArray * allKeys = [dictionaryObject allKeys];
NSLog(@"Count : %d", [allKeys count]);

最新更新