2011-06-15 57 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