1

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 来获取“Docs”数组(Docs)的标题并获取 docs 数组中的数组数量?谢谢。我对如何做到这一点感到非常困惑。

4

1 回答 1

3

像这样的东西:

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]);
于 2012-11-04T04:59:27.170 回答