WhoaItsAFactorial
WhoaItsAFactorial

Reputation: 3558

What is the key for my NSDictionary

I have created an NSDictionary from a JSON file, but when I try to do

NSString *key = [NSString stringWithFormat:@"%i",indexPath.row];
NSDictionary *currentObject = [JSONdata objectForKey:key];

I error receive and error of -[__NSCFArray objectForKey:]: unrecognized selector sent to instance

When I do an NSLog of JSONdata this is my output:

(
    {
        1 =         {
            description = "";
            facets =             (
                                {
                    name = Red;
                },
                                {
                    name = Blue;
                },
                                {
                    name = Skinny;
                },
                                {
                    name = Standard;
                },
                                {
                    name = "Navy Blue";
                }
            );
            id = 1073;
            owner = 1001;
            "post_date" = 1341980987;
            transaction = 24;
            username = TonyB;
        };
    },
    {
        2 =         {
            description = "";
            facets =             (
                                {
                    name = "Bow Tie";
                },
                                {
                    name = Blue;
                },
                                {
                    name = Orange;
                },
                                {
                    name = Yellow;
                }
            );
            id = 1001;
            owner = 1001;
            "post_date" = 1340640012;
            transaction = 6;
            username = TonyB;
        };
    }
)

Am I correcting in thinking that the first set of keys that I should be able to get should be 1 and 2 using [JSONdata objectForKey:@"0"] or [JSONdata objectForKey:@"1"]? Or am I missing something?

Upvotes: 3

Views: 166

Answers (4)

Rob
Rob

Reputation: 437392

That's a very curious JSON layout. This is an array of dictionaries, of which each dictionary has one key (that's different for each), whose object is a dictionary. I'd suggest a simple array of dictionaries.

Thus, you might have the following PHP. I assume you're reading from a database or something, but it will give you a sense of what JSON structure I'm suggesting, where this is simply an array of dictionaries. I also simplified facets, too, but whether you go that far is up to you. But the key issue is that the PHP is just generating a simple array of dictionaries:

<?php

    $object1 = array(
                   "description" => "", 
                   "facets" => array (
                       "Red",
                       "Blue",
                       "Skinny",
                       "Standard",
                       "Navy Blue"
                    ),
                    "id" => 1073,
                    "owner" => 1001,
                    "post_date" => 1341980987,
                    "transaction" => 24,
                    "username" => "TonyB");

    $object2 = array(
                   "description" => "", 
                   "facets" => array (
                       "Bow tie",
                       "Blue",
                       "Orange",
                       "Yellow"
                    ),
                    "id" => 1071,
                    "owner" => 1001,
                    "post_date" => 1340640012,
                    "transaction" => 6,
                    "username" => "TonyB");

    $results = array($object1, $object2);

    echo json_encode($results);

?>

That will generate the following JSON (which I've prettified):

[
    {
        "description" : "",
        "facets" : [
             "Red",
             "Blue",
             "Skinny",
             "Standard",
             "Navy Blue"
        ],
        "id" : 1073,
        "owner" : 1001,
        "post_date" : 1341980987,
        "transaction" : 24,
        "username" : "TonyB"
    },
    {
        "description" : "",
        "facets" : [
             "Bow tie",
             "Blue",
             "Orange",
             "Yellow"
        ],
        "id" : 1071,
        "owner" : 1001,
        "post_date" : 1340640012,
        "transaction" : 6,
        "username" : "TonyB"
    }
]

You can then read this JSON the standard way:

NSData *data = [NSData dataWithContentsOfURL:url];

NSError *error;
NSArray *jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

Finally, your cellForRowAtIndexPath or didSelectRowAtIndexPath can now just simply do:

NSDictionary *currentObject = jsonData[indexPath.row];

Or, if you're using a version of Xcode earlier than version 4.5, you would:

NSDictionary *currentObject = [jsonData objectAtIndex:indexPath.row];

Upvotes: 0

iDev
iDev

Reputation: 23278

Try this,

NSArray *currentObject = [JSONdata valueForKey:key];

Your JSONdata is an Array and not Dictionary. So that doesnt have any objectForKey method. The above one will invoke valueForKey on its objects and will return the result as an Array. If you just want to access the first object, you can directly use [[JSONdata objectAtIndex:0] objectForKey:key]; too. If the above one is used, just use

NSDictionary *currentDict = [currentObject objectAtIndex:0];

Ideally a simple solution would look like this,

NSString *key = [NSString stringWithFormat:@"%i",indexPath.row];
NSDictionary *currentObject = [[JSONdata objectAtIndex:(indexPath.row - 1)] valueForKey:key];

Upvotes: 0

Srikar Appalaraju
Srikar Appalaraju

Reputation: 73588

You have NSArray then NSDictionary. Also You are sending key as NSString where as it is a int in json. Could that be it ?

does this help [[JSONdata objectAtIndex:0] objectForKey:@"1"];

Upvotes: 2

Vincent Bernier
Vincent Bernier

Reputation: 8664

The structure of your JSON is as Follow

NSArray containing NSDictionary

So you need to do this : [[JSONdata objectAtIndex:0] objectForKey:@"1"]


Also when in doubt you can always do this :
NSLog(@"JSONdata class == %@", [JSONdata class]);

And when you hit a NSDictionary you always have this method : - (NSArray *)allKeys that will return you an array of all the key of that dictionary.

Upvotes: 3

Related Questions