Jason
Jason

Reputation: 109

Question about JSONKit analyze

First look at my json file, like this:

[
  {
    id: 1
    arr: {
        name: "功夫"
        sex: "m"
        age: 40
    }
    GP: [
        {
            Peter: 32
        }
    ]
  }
]

I used JSONKit analyze this file. and like this code

JSONDecoder *jsonDec = [[[JSONDecoder alloc] initWithParseOptions:JKParseOptionNone] autorelease];

dic = [jsonDec objectWithData:data];
if (dic == nil) {
    NSLog(@"dic is nil !!!!!!!!!");
    return;
}

NSLog(@"%d..",[dic count]);
NSLog(@"%@",dic);

if (dic != nil) {
    for (id ary in dic) {
        NSLog(@"kkkkkk %@",[ary objectForKey:@"arr"]);
    }

    for (id key in dic)
    {

        NSLog(@"key: %@ ,value: %@",key,[dic objectForKey:key]);
                    //this line error.
    } 
}

when the runtime, it crash.

2011-07-28 19:24:27.499 MyJSONTest[27274:207] -[JKArray objectForKey:]: unrecognized selector sent to instance 0x692ee40
2011-07-28 19:24:27.500 MyJSONTest[27274:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[JKArray objectForKey:]: unrecognized selector sent to instance 0x692ee40'

I need some help, thanks all.

Upvotes: 3

Views: 3090

Answers (1)

Nils Munch
Nils Munch

Reputation: 8845

It looks like the code you are running has no idea that JKArray has a objectForKey: function.

But are you sure that JKArray has a objectForKey: ? are you not thinking about JKDictionary?

NSDirectory runs with keys, and NSArrays have objectatindex:

Check the head of your file. Have you included the right file ? adding the @class header is not enough if you want to use NSObject functionality.

Upvotes: 1

Related Questions