2011-06-14 60 views
-1

我正在使用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);   
    } 
+3

您希望我們能夠找到一個代碼格式化代碼格式化問題,您在粘貼你的問題? – 2011-06-14 07:24:50

+0

我認爲:'[request responseString];'是你出錯的地方。本指南可能會有所幫助:http://blog.zachwaugh.com/post/309924609/how-to-use-json-in-cocoaobjective-c請格式化您的代碼! – 2011-06-14 07:31:59

+1

您使用NSURLConnection還是ASIHttpRequest。看起來你正在同時使用兩者都沒有。 – rckoenes 2011-06-14 07:47:07

回答

0

JSON響應字符串不一個有效的,這就是你得到錯誤的原因。

使用下面的鏈接,以檢查是否JSON字符串,從Web服務發送的響應有效的一個或不

JSONLint

+0

沒有其合法之一,但我在ASIHTTPRequest是 得到錯誤「預期類型」和警告tat「responseString找不到返回類型ID」 – Abhilash 2011-06-14 09:04:33

+0

這是一個無效的...我使用JSONLint驗證程序檢查了代碼中指定的URL,它返回的是無效的JSON字符串。 – Swapna 2011-06-14 09:38:00

+0

嘿,我得到了所有錯誤清除,但程序沒有運行我高度編輯錯誤,並粘貼我正在開始... PLZ檢查它,如果URL是錯誤的PLZ給我的URL字符串,我可以得到解析響應字符串... PLZ我沒有得到它 – Abhilash 2011-06-14 10:06:10