2012-01-26 25 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服务有问题。调试,你会发现这个问题。