2012-01-26 23 views
0

當我嘗試解析JSON響應,我得到了一個空的響應與此錯誤:我的應用程序沒有解析JSON響應

JSONValue failed. Error trace is: (
    "Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x797c420 {NSLocalizedDescription=Unexpected end of string}" 
) 

這是我嘗試發送JSON請求和我怎樣努力解析其響應:

發送JSON請求:

- (void)viewWillAppear:(BOOL)animated{ 
    //Specify the adress of the webservice (url) 
    NSURL *url = [NSURL URLWithString:@"http://xxxxxxxxxxx.com/webservices/"]; 
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    NSString *jsonStringArray=[aMutableArray JSONRepresentation]; 

    NSLog(@"-------------"); 
    NSLog(@"%@",jsonStringArray); 
    NSLog(@"-------------"); 
    [request setPostValue:jsonStringArray forKey:@"liste_des_themes"]; 
    NSLog(@"The response string is: %@",request.responseString); 
    [request setDelegate:self]; 
    [request startAsynchronous]; 
} 

對於NSLog這裏我想送它我得到一個正確的JSON格式之前顯示JSON字符串:

NSLog(@"%@",jsonStringArray);//["Mairie","Préfectures et sous-préfectures"] 

現在第二NSLog,我得到空:

NSLog(@"The response string is: %@",request.responseString);//The response string is: (null) 

當解析響應:

-(void)requestFinished:(ASIHTTPRequest *)request 
    { 
     if(request.responseStatusCode==200) 
     { 
     NSLog(@"This block gets called, response code is 200");//This block gets called, response code is 200 
     //parse the response 
      NSLog(@"The response string is: %@",request.responseString); 

      NSString *responseString=[request responseString]; 
      NSDictionary *responseDict=[responseString JSONValue]; 
    } 
} 

對於NSLog試圖顯示響應字符串,我什麼也沒得到,既不爲零也不正確:

  NSLog(@"The response string is: %@",request.responseString); 
    //The response string is: 

而在這之後,我得到這個錯誤跟蹤:

JSONValue failed. Error trace is: (
     "Error Domain=org.brautaset.JSON.ErrorDomain Code=11 \"Unexpected end of string\" UserInfo=0x797c420 {NSLocalizedDescription=Unexpected end of string}" 
    ) 

你能幫助我嗎? thanx提前。

+1

對我來說,它看起來像一個服務器端問題 – vikingosegundo

回答

2

我假設你對Web服務沒有控制權,所以告訴你要做些什麼通常是不起作用的。

1)首先,確保JSON響應看起來正確。你可以下載到瀏覽器並看到它看起來好嗎?

2)如果它看起來沒問題,它可以通過JSON皮棉測試嗎? (嘗試jsonlint.com,並將其插入。)

3)如果它通過(1)和(2),則查看返回的數據響應中是否有任何前導空值。我發現這是我自己的代碼中的情況。如果沒有先刪除主要的空值,我就無法直接處理這個問題。

不幸的是,我不是在我的Mac上覆制我用來解決這個問題的代碼。但這是一個真正的問題,解決辦法是刪除空值(在我的例子中有成千上萬個空值)。空值看起來像字符串終結符,所以前導空值隱藏真實內容。

+0

你好吉姆,我有所有在web服務端的控制權,它是我的,JSON響應是好的,我已經在'jsonlint.com'上測試過了,它仍然是你答覆中的第3步,我不明白你的意思是什麼,因爲這是我的迴應:'{「themes」:[[[「方向des路由Secteur de Pithiviers」,「mairies」, 「Morailles 45300 PITHIVIERS LE VIEIL」,「0238345744」,「48.823042」,「2.365907」],[「Cr \ u00e9dit Mutuel Du Center」,「banques」,「agence de Pithiviers 33 r Am Gourdon 45300 PITHIVIERS」,「0820834080」 ,「48.703042」,「2.145907」]]}' – Malloc

+0

出於某種原因,我使用的前綴字節值爲0x00。有很多。我無法控制發送的內容。它在瀏覽器中看起來很完美,所以我認爲瀏覽器會將這些空值刪除。但是當我檢查我的程序收到的原始數據時,零顯而易見,並被解釋爲字符串的空終止符。檢查代碼中的URL連接收到的原始數據。 – Jim

+0

因此,響應字符串上的零可能導致問題? – Malloc

1

您對提供JSON的Web服務有問題。調試,你會發現這個問題。