2011-06-15 72 views
0

我正在使用以下代碼,並且我正在JSON中獲取響應字符串。我不知道如何解析它。我應該怎麼做未來,如果我想打電話JSON輸出的任何方法如何處理來自json輸出的responseString並解析它

//源代碼

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    dataWebService = [[NSMutableData data] retain]; 

    NSMutableURLRequest *request = [[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.googleapis.com/customsearch/v1?key=AIzaSyDzl0Ozijg2C47iYfKgBWWkAbZE_wCJ-2U&cx=017576662512468239146:omuauf_lfve&q=lectures&callback=handleResponse"]]retain];  

    NSURLConnection *myConnection = [NSURLConnection connectionWithRequest:request delegate:self]; 

    [myConnection start];  
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [dataWebService setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [dataWebService appendData:data]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSString *responseString = [[NSString alloc] initWithData:dataWebService encoding:NSUTF8StringEncoding]; 
    NSLog(@"Response: %@",responseString); 

    [responseString release]; 
    [dataWebService release]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Error during connection: %@", [error description]); 
} 

回答

0

使用JSON解析框架:http://code.google.com/p/json-framework/

+0

感謝ü烏拉圭回合響應我得到的輸出,但如何解析教程在下面提供的網站http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/76730-webservice-how.html – Abhilash 2011-06-15 07:09:59

+0

是的,就像這樣:NSDictionary * dictionary = [responseString JSONValue];不容易得多 – Steve 2011-06-15 07:12:16

+0

在哪個方法我shd調用NSDictionary *字典?我是新來的iPhone我不知道太多的代碼.. – Abhilash 2011-06-15 08:44:17