iPhone, SBJSON error



我正在使用SBJson Parser这个

我有这个JSON值,它是一个有效的JSON,但我仍然得到这个错误

-JSONValue failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=3 "Unrecognised leading character" UserInfo=0x63726a0 {NSLocalizedDescription=Unrecognised leading character}"

{
"Account": {
    "LoginName": "My Name",
    "Name": "My Name"
},
"UseInvoiceAddressAsDeliveryAddress": "true",
"InvoiceAddress": {
    "Zip": "16444",
    "CountryId": "1",
    "City": "SSSS",
    "Line2": "8",
    "Line1": "Street 4"
},
"Phone": "12345678",
"FirstName": "My",
"LastName": "Name",
"Email": "sample@example.com",
"CellPhone": "234254233"
}

这是我的代码,我想我得到了这个JSON错误,因为我没有指定任何内容类型,我正在这样做,但它仍然不起作用

    SBJsonWriter *writer = [[SBJsonWriter alloc] init];
NSString *jsonConvertedObj = [writer stringWithObject:customerObject];
NSLog(@"The converted JSON String .... %@",jsonConvertedObj);
NSData *postData = [jsonConvertedObj dataUsingEncoding:NSASCIIStringEncoding];  
NSMutableData *myMutablePostData = [NSMutableData dataWithData:postData];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:myURL];
[request setRequestMethod:@"POST"];
[request setClientCertificateIdentity:identity];
[request setValidatesSecureCertificate:NO];
[request addData:myMutablePostData withFileName:@"" andContentType:@"application/json" forKey:@""];
[request startSynchronous];

这是我从服务器收到的错误:

The server encountered an error processing the request. The exception message is 'The     incoming message has an unexpected message format 'Raw'. The expected message formats for the operation are 'Xml'; 'Json'. This can be because a WebContentTypeMapper has not been configured on the binding.

我自己解决了,这就是解决它的原因。。。

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:myURL];
[request setPostBody:myMutablePostData];
[request setRequestMethod:@"POST"];
[request setClientCertificateIdentity:identity];
[request setValidatesSecureCertificate:NO];
[request addRequestHeader:@"Content-Type" value:@"application/json"];
[request setDelegate:self];
[request startSynchronous];

正如所怀疑的那样,我使用了错误的HTTPRequest类型,然后我没有设置内容类型。

好吧,我想你可能有一些看不见的非法字符。这也有可能是您正在使用的库中的一个错误。所以我建议你试着用一个不同的库来解析它。如果JSON有问题,那么其他库也会出现类似的错误。如果其他库解析您的JSON没有问题,您可能应该为SBJSON提交一份错误报告。

我使用JSONKit,它对我来说一直很好用,而且它真的很容易使用。

从粘贴的内容来看,在JSON启动之前似乎有一些换行符。如果真的是这样的话,这可能就是你出现错误的原因。

最新更新