views:

33

answers:

2

I've been trying to make this work for some time now, but I think I need someone on the outside to see what I'm doing wrong. In my app, I'm getting a JSON value from a web server, and parsing it into my table view.

responseData = [NSData dataWithContentsOfURL: [NSURL URLWithString: @"http://myserver/json"]];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSDictionary *dict = [responseString JSONValue];
theData = [dict objectForKey:@"data"];

My only problem now, is that 'theData' is apparently a NSString, as far as I am able to see. The following error is what I get from GDB when I call '[theData count]' from my view controller.

-[NSCFString count]: unrecognized selector sent to instance 0x6eb5380

And what my JSON looks like

{
    "code": 100,
    "data": [
    {
        /** Some object */
    }]
}
A: 

You can test if its an NSString with:

[theData isKindOfClass:[NSString class]]

Also why are you using NSCFString instead of NSString? NSString does not have a count method! If you want the length of a string its simply:

[theData length];
ennuikiller
Using the test-method, I can see that theData was actually a string when it was supposed to be an array. I also found out that the JSON parser I used didn't work as expected. Using another JSON parser that works, I got the result I wanted, with an array.
eriktm
A: 

Have you tried enumerating through the dictionary "dict" to see if all keys & values are what you expect?

Satyajit