如何从webservice解析json responseString

时间:2011-06-14 07:19:44

标签: iphone objective-c xcode

我使用json连接到webservices。

我使用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);        
    }

1 个答案:

答案 0 :(得分:0)

JSON响应字符串不是有效字符串,这就是您收到错误的原因。

使用以下链接检查作为Web服务响应发送的JSON字符串是否有效

JSONLint