2012-09-19 75 views
0

我在其中我有這樣的JSON響應的應用程序:如何在iphone中解析json響應?

{ 
    "status":"ok", 
    "dfs":[{"type":"i", 
    "title":"image", 
    "image_path":"https:\/\/s3.dhgdfhgfhgdhgfh\/ad_226.png"}] 
} 

,我分析是這樣的:

NSDictionary *dict=[[request responseString] JSONValue];     
NSLog(@"dict %@ ",[request responseString]); 

if([[dict objectForKey:@"status"] isEqualToString:@"ok"]) 
{     
    NSMutableDictionary *dict1=[dict objectForKey:@"dfs"]; 

    NSLog(@"%@",[dict1 classForCoder]); 
    NSLog(@"%@dict1",dict1); 
    titlelbl.text=[dict1 objectForKey:@"title"]; 

    if ([[dict1 objectForKey:@"type"] isEqualToString:@"i"]) 
    { 
     ad.hidden=NO; 

     NSString *imageurl=[dict1 objectForKey:@"image_path"]; 

     NSLog(@"%@",imageurl); 
    } 

但我得到這個錯誤:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM objectForKey:]: unrecognized selector sent to instance 0xde7ecf0' .

我得到dict1類文件夾爲nsmutable數組。任何人都可以幫助我實現這個目標嗎?

回答

2
{ 
"status":"ok", 
"dfs": 
[ 
{ 
"type":"i", 
"title":"image ", 
"image_path":"https:\/\/s3.dhgdfhgfhgdhgfh\/ad_226.png" 
} 
] 
} 

讀JSON響應如上,它的一個詞典,其內的第一對象是字符串,第二是字典的陣列(其只有一個對象)。

NSDictionary *dict = [[request responseString] JSONValue]; 

if([[dict objectForKey:@"status"] isEqualToString:@"ok"]) 
{ 
    NSArray* arr = [dict objectForKey:@"dfs"]; 
    NSDictionary* dict1 = [arr objectAtIndex:0]; 
enter code here 
enter code here 
enter code here 
enter code here 

// get the data from dict1 with the key value pairs, and set labels and images 
} 
+0

titlelbl.text = [dict1 objectForKey:@「title」];這會工作嗎? – hacker

+0

是的,在爲什麼我已經註釋掉的地方,寫在der上,它會工作.. – vishy

+0

ohhh..thats working .. thanks buddy .. – hacker