tags:

views:

39

answers:

2

example 1: ==> [{"name":"luxy"}] example 2: ==> {"name":"luxy"}

Both example I got are valid json format...

in xcode... I write below:

[dictionary objectForKey:@"name"]

in example 2 I can get "luxy" but how come if I use example 1 then it fails? is my xcode wrong?

A: 

I'm guessing that you're using stig's json-framework? If so, when you parse example one you get an NSArray. When you parse example two, you get an NSDictionary.

id example1 = [parser objectWithString:jsonString];

if ([example1 isKindOfClass:[NSArray class]]) {
    // You've got an array, probably an array of dictionaries
} else if ([example1 isKindOfClass:[NSDictionary class]]) {
    // you've got a dictionary, handle it
} else {
    // you've got something else
}
kubi
A: 

Thanks to Kubi!!! You are right! forgot the basic json logic about array & object difference...

I am quite new into xcode but kinda of weird.... if is array ...

NSString *element = [example1 objectAtIndex:0];

I can get the string from element... but weird when I put

NSDictionary *dictionary = [element JSONValue]; <== at this stage I get some unrecongized selector.....

[dictionary objectForKey:@"name"]

CBY