0

Hi I been having huge issues grabbing some data out of this for me quite complex JSON structure.

The problem is that the JSON structure contains some nesting that i can't find out how to handle.

Example of the JSON i get from the database:

    {
        "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
                    }
...

My job is to get all the "Name"s from all posts where the "Status" is set to "usable". And store them in my app. But I'm having a hard time getting that info . Im new to JSON and can't handle the structure. This is what i got so far after hours of googling:

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); 
}}   

From that code I'm able to get the ID from the first item. But I don't seem to be able to fetch the "name" or check if they are "usable". Have been working with this and trying to solve this for hours but this is above my leauge. I guess the reason to why I can't solve it is cause the data is nested in hits.hits etc. Yet I have to solve this to be able to finish my app. I would be very grateful for some help.

Here is a longer version of the JSON database. The full version contains over 3000 items: http://jsonviewer.stack.hu/#http://www.pumba.se/example.json

2
  • 1
    That is very complicated JSON. Many of those properties are in arrays even though they only contain one element. If you can have any influence on the JSON then please exert it now in order to simplify it. WRT to answering your question, it appears you are unfamiliar with how embedded collection classes in Objective-C are used? Commented Feb 18, 2016 at 10:36
  • I will try to speak to the guys in charge of the JSON, but I'm not sure that they can make it more simple. Yes I'm afraid i am unfamiliar with how embedded collection classes in Objective-C are used :/ Commented Feb 18, 2016 at 10:41

1 Answer 1

2

You can use the following code:

 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);
}}
Sign up to request clarification or add additional context in comments.

1 Comment

Looks good to me. @Dlindau Observe the need to use firstObject in order to get the sole occupant of the value arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.