我正在使用json連接到webservices。如何從webservice解析json responseString
我使用nsurl連接進行連接。我得到了json響應字符串...我需要解析它。
當我嘗試分析它我收到以下錯誤:
使dyld:找不到符號:_CFXMLNodeGetInfoPtr
這裏是相關的源代碼:
// Using NSURLConnection
- (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];
[super viewDidLoad];
}
- (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]);
}
// Parse JSON
- (void)requestCompleted:(ASIHTTPRequest *)request
{
NSString *responseString = [request responseString];
NSDictionary *dictionary = [responseString JSONValue];
NSDictionary *dictionaryReturn = (NSDictionary*) [dictionary objectForKey:@"request"];
NSString *total = (NSString*) [dictionaryReturn objectForKey:@"totalResults"];
NSLog(@"totalResults: %@", total);
int count = [[dictionaryReturn objectForKey:@"count"] intValue];
NSLog(@"count: %d", count);
}
您希望我們能夠找到一個代碼格式化代碼格式化問題,您在粘貼你的問題? – 2011-06-14 07:24:50
我認爲:'[request responseString];'是你出錯的地方。本指南可能會有所幫助:http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c請格式化您的代碼! – 2011-06-14 07:31:59
您使用NSURLConnection還是ASIHttpRequest。看起來你正在同時使用兩者都沒有。 – rckoenes 2011-06-14 07:47:07