2012-03-14 23 views
0

Facebookのグラフ要求によって返されたJSONを解析しています。 jsonというNSMutableArrayを作成してリクエストの結果を保持し、JSON形式の情報を返します。 次に、解析された情報をテーブルに追加しようとします。しかし、私はNSMutableArrayのは、それを合成し、以下のように、それを初期化するよう、私はヘッダファイルにself.jsonを作成JSONエラーの解析

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

エラーが出る

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

を:これを行うには、私が使用してNSDictionaryのを作成します:

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]; 

私も助けにはならなかったNSMutableArrayの、にNSJSONSerializationから返されたデータをキャストしようとしています。

それは self.jsonは実際にはもちろん-objectAtIndexにrespondeしないNSMutableArrayのが、NSDictionaryのを、保持していないことを、非常に明白であるエラーメッセージからまあ
+0

jsonフレームワークを使用してjson配列を解析します。 https://github.com/stig/json-framework。 – Neo

+0

それはうまく解析しています--jsonライブラリは問題ではありません:) – deanWombourne

答えて

1

それはおそらくサービスので、辞書を含むされますJSON-Objectを返します。応答(NSLog(@"%@", self.json)self.json = ...の直後)を見て、コードを適切に調整してください。

1

Facebookは辞書ではなく配列を返しています。

戻ってくるデータは、「{」ではなく「a」で始まることに注意してください。

0

あなたはすでにあなたの参照のためにまだ答えが得られたらいいですか?辞書はちょうどJSON構造のようです。 JSONオブジェクトの例は{ "key1" : "value1" , "key2" : "value2" ,"key3" : "value3" , "key4" : "value4" }です(JSONについて詳しく知りたい場合はread hereを参照してください)。また、Dictionaryオブジェクトには明らかにkey:valueのペアが保持されています。

-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]; 
} 
+0

これは本当に適切な答えです。 OP用のコードを書いてはいけませんが、彼が何を間違えたのか、どうやって修正できるのかを教えてください。また、あなたのコードはあなたが言及していないライブラリを使用しており、また、コードには全く質問/問題に関連していない非常に多くのものが含まれています。 – Ahti