Ios 解析JSON响应时出错,错误3840
当我想将json发布到web服务器时,它将崩溃并显示:Ios 解析JSON响应时出错,错误3840,ios,objective-c,json,post,Ios,Objective C,Json,Post,当我想将json发布到web服务器时,它将崩溃并显示: Warning, status code of response was not 200, it was 400 2014-03-20 01:15:54.407 iBeacons[6669:60b] error parsing JSON response: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error
Warning, status code of response was not 200, it was 400
2014-03-20 01:15:54.407 iBeacons[6669:60b] error parsing JSON response: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (JSON text did not start with array or object and option to allow fragments not set.) UserInfo=0x156b6440 {NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
2014-03-20 01:15:54.408 iBeacons[6669:60b]返回字符串:
400所需字符串参数“beaconsMapping”不存在所需字符串参数“beaconsMapping”不存在
if ([NSJSONSerialization isValidJSONObject:sendData])
{
NSError *error;
NSLog(@"1");
NSData *jsonData=[NSJSONSerialization
dataWithJSONObject:sendData options:
NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:&error];
NSLog(@"2");
NSString *json=[[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"%@",json);
NSString *reqData=[@"beaconsMapping=" stringByAppendingString:json];
NSLog(@"%@",reqData);
NSData *postDatas=[NSData dataWithBytes:[reqData UTF8String] length:[reqData length]];
NSString *postLength=[NSString stringWithFormat:@"%d",[postDatas length]];
NSMutableURLRequest *requestPost=[NSMutableURLRequest requestWithURL:dataUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSLog(@"%@",dataUrl);
[requestPost setHTTPMethod:@"POST"];
[requestPost setValue:postLength forHTTPHeaderField:@"Content-Length"];
[requestPost setValue:@"application/json"forHTTPHeaderField:@"Accept"];
[requestPost setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
[requestPost setHTTPBody:postDatas];
NSError *requestError=nil;
NSURLResponse *response = nil;
NSData *data=[NSURLConnection sendSynchronousRequest:requestPost returningResponse:&response error:&requestError];
if (requestError == nil) {
if ([response isKindOfClass:[NSHTTPURLResponse class]]) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (statusCode != 200) {
NSLog(@"Warning, status code of response was not 200, it was %d", statusCode);
}
}
NSError *error;
NSDictionary *returnDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (returnDictionary) {
NSLog(@"returnDictionary=%@", returnDictionary);
} else {
NSLog(@"error parsing JSON response: %@", error);
NSString *returnString = [[NSString alloc] initWithBytes:[data bytes] length:[data length] encoding:NSUTF8StringEncoding];
NSLog(@"returnString: %@", returnString);
}
} else {
NSLog(@"NSURLConnection sendSynchronousRequest error: %@", requestError);
}
}
//我的json输出:
beaconMapping=[
{
"floorplanId": "mcc",
"id": "mcc.b",
"beaconIds": [
{
"distance": "3.873",
"beaconId": "identityId:Major: 44643, Minor: 29271 x:23.6 y:-21.3 z:-50.2"
}
],
"locationId": "b"
}
]//thewebserverrequirestheformatlikethat
[
{
"floorplanId": "mcc",
"locationId": "c",
"id": "mcc.c",
"beaconIds": [
]
},
{
"floorplanId": "mcc",
"locationId": "b",
"id": "mcc.b",
"beaconIds": [
{
"beaconId": "identityId:Major: 44643, Minor: 29271 x:58.0 y:124.6 z:-360.3",
"distance": "3.202"
}
]
},
{
"floorplanId": "mcc",
"locationId": "a",
"id": "mcc.a",
"beaconIds": [
]
}
]
//我的网页邮编:
@RequestMapping(value = "/beacons/{floorplanId}", method = RequestMethod.POST)
public @ResponseBody
Set<BeaconsAtFloorplanLocation> setBeacons(
@PathVariable("floorplanId") String floorplanId,
@RequestParam("beaconsMapping") String beaconMappingJson)
throws Exception {
Set<BeaconsAtFloorplanLocation> beacons = (new MCCObjectMapper())
.readValue(beaconMappingJson,
new TypeReference<Set<BeaconsAtFloorplanLocation>>() {
});
beaconsLoader_.setBeaconMapping(floorplanId, beacons);
return beacons;
}
@RequestMapping(value=“/beacons/{floorplanId}”,method=RequestMethod.POST)
公共@ResponseBody
设置挫折(
@PathVariable(“floorplanId”)字符串floorplanId,
@RequestParam(“BeaconMapping”)字符串beaconMappingJson)
抛出异常{
设置信标=(新的McObjectMapper())
.readValue(beaconMappingJson,
新类型引用(){
});
信标加载器。设置信标映射(楼层平面ID,信标);
返回信标;
}
在编码json时,您可能没有提供正确的格式数据
您正在使用json序列化吗
尝试使用NSLog查看数据是否正确,然后再创建一个NSError*错误,并将其放入json编码中,以检查发生了什么,然后再将其发布到Web服务器 我已经解决了这个问题,删除
[requestPost setValue:@"application/json"forHTTPHeaderField:@"Accept"];
[requestPost setValue:@"application/json"forHTTPHeaderField:@"Content-Type"];
它会起作用的 尝试删除
Content-Length
Accept
及
从你的代码。这可能对你有帮助。
虽然我的json对象是有效的json,但json WCF也有同样的问题。
我从代码中删除了所有这3个元素,它成功了。服务器没有接受这些。
我不知道为什么
干杯如果您连接到设备上的VPN,您可能会遇到此问题 发布您的相关代码可能是json编码发现了问题。我添加了代码,请查看,thanksbeaconMapping=equal符号在json格式中无效,但我的http帖子需要一个名为beaconMapping的参数来接收发送的数据,我应该如何处理它。我已经在上面发送了java web服务器代码是的,当输出数据看起来正确时,我发布了我的代码和json格式,你能帮我再次检查一下吗?谢谢
Content-Type