0

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.

2
  • Use json framework to parse json array. github.com/stig/json-framework. Commented Mar 14, 2012 at 4:15
  • It's parsing fine - the json library isn't the problem :) Commented Mar 14, 2012 at 6:13

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Facebook is returning a dictionary and not an array.

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

Comments

0

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.

Comments

-1
-(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];
}

1 Comment

This is really no appropriate answer at all. You should not write code for OP, but tell him what he did wrong and how he can fix it, possibly using some code examples. Also, your code is using libraries that you dont mention, and also the code contains very much stuff that is not at all related to the question/problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.