glenn sayers
glenn sayers

Reputation: 2414

Parsing JSON error

I am parsing JSON returned by Facebooks Graph request. I create an NSMutableArray called json to hold the results of the request, which returns information in JSON format. I then try to add the information parsed to a table. To do this, I create an NSDictionary using:

NSDictionary *dict = [self.json objectAtIndex:0];

However, I get the error

-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6b2e1c0

I create self.json in the header file as a NSMutableArray synthesize it, and initialize it as follows:

NSURL *url = [NSURL URLWithString:@"https://graph.facebook.com/40796308305/albums"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSError *err;
self.json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&err];

I have also tried casting the data returned from NSJSONSerialization to an NSMutableArray, which didn't help.

Upvotes: 0

Views: 234

Answers (4)

Vibhooti
Vibhooti

Reputation: 1203

-(void) jsonRESTWebServiceMethod:(NSString*)method WithParameter:(NSMutableDictionary*)params{
    self.iResponseType = JSONTYPE;
    NSMutableDictionary *bodyDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:method,@"name",params,@"body",nil,nil];
    NSData *data = [[CJSONSerializer serializer] serializeObject:bodyDict error:nil];
    NSString *strRequest = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
    strRequest = [NSString stringWithFormat:@"json=%@",strRequest];
    NSData *body = [strRequest dataUsingEncoding:NSUTF8StringEncoding];
    NSString *strURL = [NSString stringWithString:WebServiceURL];
    NSString* escapedUrlString = [strURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

    NSMutableURLRequest *request= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:escapedUrlString]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:body];
    NSString *msgLength = [NSString stringWithFormat:@"%d",strRequest.length];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField: @"Content-Type"];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];

    if (mydata) {
        [mydata release];
        mydata = nil;
    }
    conections = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    mydata = [[NSMutableData alloc] init];
}

-(void)connectionFinish:(WebService*)webservice response:(NSData*)data{
       NSMutableDictionary *dctResponse = [[CJSONDeserializer deserializer] deserialize:data error:nil];
}

Upvotes: -1

iDroid
iDroid

Reputation: 1140

Hope you already got the answer still for your reference The Dictionary Is just looks like the JSON Structure. Example JSON Object is { "key1" : "value1" , "key2" : "value2" ,"key3" : "value3" , "key4" : "value4" } (just for reference read hereto know more about JSON) and the Dictionary Object obviously holds the key:value pairs.

Upvotes: 0

EricS
EricS

Reputation: 9768

Facebook is returning a dictionary and not an array.

Note that the data that's coming back starts with a '{' and not a '['.

Upvotes: 1

Ahti
Ahti

Reputation: 1430

Well from the error message it is quite obvious, that self.json does in fact not hold a NSMutableArray but a NSDictionary, which of course does not responde to -objectAtIndex:

It is containing a Dictionary probably because the service returns a JSON-Object. Take a look at the response (NSLog(@"%@", self.json) right after self.json = ...) and adjust your code appropriately.

Upvotes: 1

Related Questions