iosobjective-csbjson

Array returned from SBJSON parser is invalid (iOS)


I am running into an error using SBJSON parser on a response string returned from a PHP webservice:

NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSError *error;
SBJSON *json = [SBJSON new];
NSArray *returnedFromWeb = [json objectWithString:responseString error:&error];

[articles_parsed removeAllObjects];

for (int i = 0; i < [returnedFromWeb count]; i++) 
{
    NSDictionary *dictColumns = [returnedFromWeb objectAtIndex:i];
    [self parseArticlesObject:dictColumns];
    dictColumns = nil;
}

I receive an unrecognized selector sent to instance error on the objectAtIndex call above. When I check the values contained in my array (returnedFromWeb), it shows that there are 17 objects. However, inspecting each object shows the following error:

(<invalid>) [0] = <error: expected ']' error: 1 errors parsing expression >

I have posted below the data returned from the SBJSON parser for the object at index [0] below:

{
    0 =     {
        author = "CCJ Staff";
        category1 = Products;
        excerpt = "Toyo";
        modified = "2014-03-25 08:33:57";
        "short_link" = "http://www.example.com/?p=95326";
        thumbnail = "http://www.example.com/files/2014/03/ Tire.jpg";
        "thumbnail_243x243" =     "http://www.example.com/ihz.php?src=/blogs.dir/10/files/2014/03/ Tire.jpg&amp;w=243&amp;h=243&amp;q=100&amp;zc=1";
        title = "medium-duty";
    };

And here is the string that is being parsed by SBJSON (shortened to contain only the first object, the comma at the end is where the next object would begin):

{"numberposts":240,"tax_query":[{"taxonomy":"topic","terms":[75,43550,14394,16564,1795,43,22,12950],"field":"term_id","orderby":"date","order":"DESC"}],"0":{"title":"medium-duty","category1":"Products", excerpt":"Toyo","modified":"2014-03-25 08:33:57","author":"Staff","thumbnail":"http://www.ccjdigital.com/files/2014/03/Tire.jpg","short_link":"http://www.ccjdigital.com/?p=95326"},

Any help is appreciated! I am fairly new to iOS and SBJSON, and have not been able to figure out why my array is invalid.


Solution

  • My first thought is that you're getting an NSDictionary back not an NSArray. To find out add a breakpoint before you call objectAtIndex on the returned item, and in the debugger at the bottom of the screen type: po [returnedFromWeb class]

    This will tell you the actual class of the returned object. I say that it might be a dictionary because the JSON you pasted in starts with a { (curly brace) where an array would start with [

    Since you can't always guarantee that the JSOn will be formatted to be an array or a dictionary you can do some testing to see if it responds to the objectAtIndex: method, as an example, which would indicate it's an NSArray.

    id *returnedFromWeb = [json objectWithString:responseString error:&error];
    if ([returnedFromWeb respondsToSelector:@selector(objectAtIndex:)]) {
      //it's an array do array things.
    } else {
      //it's a dictionary do dictionary things.
    }
    

    Hope that helps!