来自 Xcode 中 JSON 的嵌套 NSDictionary


嗨,我遇到了

巨大的问题,为我提供了相当复杂的JSON结构。

问题是 JSON 结构包含一些我无法找到如何处理的嵌套。

我从数据库中获取的 JSON 示例:

    {
        "hits": {
            "totalHits": 3202,
            "hits": [{
                        "id": "70132eb7-2834-458c-900a-da951c95a506",
                        "versions": [{
                            "id": 7,
                            "properties": {
                                "Status": [
                                    "usable"
                                ],
                                "created": [
                                    "2015-10-27T14:31:13Z"
                                ],
                                "Text": [
                                    "Snabbtåg, Järnväg, Höghastighetsjärnväg, "
                                ],
                                "ConceptDefinitionLong": [
                                    "Enligt Trafikverket saknas en helt entydig och vedertagen definition av höghastigheteståg."
                                ],
                                "contenttype": [
                                    "Concept"
                                ],
                                "ConceptProposalUser": [
                                    "[object Object]"
                                ],
                                "ConceptType": [
                                    "object"
                                ],
                                "Name": [
                                    "Höghastighetståg"
                                ],
                                "ConceptNote": null,
                                "ConceptSeeAlso": null,
                                "Note": null,
                                "ConceptName": [
                                    "Höghastighetståg"
                                ],
                                "updated": [
                                    "2016-02-01T11:37:30Z"
                                ],
                                "ConceptDefinitionShort": [
                                    "Snabbtåg, Järnväg, Höghastighetsjärnväg, Kollektivtrafik"
                                ],
                                "ConceptStatus": [
                                    "usable"
                                ]
                            }
                        }],
                        "noVersions": 1
                    }, {
                        "id": "4224ccfb-1f0a-9491-727f-f6ab0fc2c951",
                        "versions": [{
                            "id": 2,
                            "properties": {
                                "Status": [
                                    "usable"
                                ],
                                "created": [
                                    "2016-01-25T12:03:33Z"
                                ],
                                "Text": [
                                    "Rosenlundsbadet öppnade 1968 och äventyrsbadet 1991."
                                ],
                                "ConceptDefinitionLong": [
                                    "Rosenlundsbadet är en badanläggning i Jönköping. "
                                ],
                                "contenttype": [
                                    "Concept"
                                ],
                                "ConceptProposalUser": null,
                                "ConceptType": [
                                    "organisation"
                                ],
                                "Name": [
                                    "Rosenlundsbadet"
                                ],
                                "ConceptNote": null,
                                "ConceptSeeAlso": null,
                                "Note": null,
                                "ConceptName": [
                                    "Rosenlundsbadet"
                                ],
                                "updated": [
                                    "2016-01-25T12:03:38Z"
                                ],
                                "ConceptDefinitionShort": [
                                    "Simning, Simhopp, Äventyrsbad"
                                ],
                                "ConceptStatus": [
                                    "usable"
                                ]
                            }
                        }],
                        "noVersions": 1
                    }
...

我的工作是从"状态"设置为"可用"的所有帖子中获取所有"名称"。并将它们存储在我的应用程序中。但是我很难获得这些信息.我是 JSON 的新手,无法处理结构。这是我在谷歌搜索数小时后得到的:

NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
                          [NSURL URLWithString:@"http://www.pumba.se/example.json"]];
NSError *error;
NSMutableDictionary *JSONdictionary = [NSJSONSerialization
                                   JSONObjectWithData:allCoursesData
                                   options:kNilOptions
                                   error:&error];
if( error )
{
    NSLog(@"%@", [error localizedDescription]);
}
else {
    NSArray* entries = [JSONdictionary valueForKeyPath:@"hits.hits"];
    NSDictionary *firstItem = [entries objectAtIndex:0];
    NSString *id1 = [firstItem objectForKey:@"id"];
    NSLog(@"ID: ");
    NSLog(id1); 
}}   

从该代码中,我能够从第一项中获取ID。但我似乎无法获取"名称"或检查它们是否"可用"。一直在研究这个问题并试图解决这个问题几个小时,但这超出了我的 leauge。我想我无法解决它的原因是因为数据嵌套在 hits.hit 等中。然而,我必须解决这个问题才能完成我的应用程序。我将非常感谢一些帮助。

下面是 JSON 数据库的较长版本。完整版包含3000多个项目:http://jsonviewer.stack.hu/#http://www.pumba.se/example.json

您可以使用以下代码:

 NSData *allCoursesData = [[NSData alloc] initWithContentsOfURL:
                          [NSURL URLWithString:@"http://www.pumba.se/example.json"]];
NSError *error;
NSMutableDictionary *JSONdictionary = [NSJSONSerialization
                                       JSONObjectWithData:allCoursesData
                                       options:kNilOptions
                                       error:&error];
if( error )
{
    NSLog(@"%@", [error localizedDescription]);
}
else {
    NSMutableArray *allNames = [NSMutableArray array];
    NSArray* entries = [JSONdictionary valueForKeyPath:@"hits.hits"];
    for (NSDictionary *hit in entries) {
        NSArray *versions = hit[@"versions"];
        for (NSDictionary *version in versions) {
            NSDictionary *properties = version[@"properties"];
            NSString *status = [properties[@"Status"] firstObject];
            NSString *name = [properties[@"Name"] firstObject];
            if ([status isEqualToString:@"usable"]) {
                [allNames addObject:name];
            }
        }
    }
    NSLog(@"All names: %@", allNames);
}}

最新更新