-JSONValue失败。错误是:意外的输入结束

时间:2012-07-11 12:08:51

标签: iphone ios json

我得到了-JSONValue failed. Error is: Unexpected end of input的JSON结果。请以正确的方式指导我。

我是解析新手。我必须通过POST方法从服务器获取数据。我有以下细节。我必须用url传递zip

{"zip":"52435","methodIdentifier":"search_dealer"}

url : http://usaautoleads.com/api.php

method: post

web service name: search_dealer

response : {"success":"0","dealer":[info...]}

我的代码在这里。

NSURL *myURL=[NSURL URLWithString:@"http://usaautoleads.com/api.php"];

NSString *post =[[NSString alloc]initWithString:@"52435"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

[request setURL:myURL]; 

[request setHTTPMethod:@"POST"];

[request setHTTPBody:postData]; 

[request setValue:@"application/json" forHTTPHeaderField:@"content-type"];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

1 个答案:

答案 0 :(得分:1)

这里的问题是你的帖子字符串。使用这个

NSString *zip = @"52435";
NSString *methodID = @"search_dealer";
NSString *post =[NSString stringWithFormat:@"{\"zip\":\"%@\",\"methodIdentifier\":\"%@\"}", zip, methodID];