iPhone,SBJSON错误

时间:2011-05-27 15:35:07

标签: iphone xcode json parsing

我正在使用SBJson Parser this one

我有这个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.

3 个答案:

答案 0 :(得分:1)

我自己解决了,这就解决了它......

    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类型,然后我没有设置内容类型。

答案 1 :(得分:0)

嗯,我想你可能有一些看不见的非法角色。这也可能是您正在使用的库中的错误。所以我建议你尝试用不同的库解析它。如果JSON出现问题,您应该会遇到与其他库类似的错误。如果其他库没有问题地解析您的JSON,您应该为SBJSON提交错误报告。

我使用JSONKit并且它总是对我有用,而且它非常易于使用。

答案 2 :(得分:0)

从你粘贴的内容看起来,在你的JSON开始之前有一些换行符。如果情况确实如此,那么可能就是您收到该错误的原因。