如何从webservice解析json responseString

问题描述

| 我正在使用json连接到Web服务。 我使用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);        
    }
    

解决方法

        JSON响应字符串不是有效的字符串,这就是您收到错误的原因。 使用以下链接检查从Web服务作为响应发送的JSON字符串是否有效。 JSONLint