Iphone JSONRepresentation显示错误

Iphone JSONRepresentation显示错误,iphone,ios,objective-c,json,ipad,Iphone,Ios,Objective C,Json,Ipad,我在字典里有一些数据,比如 NSMutableDictionary *jsonDictionary; jsonDictionary = [[NSMutableDictionary alloc] init]; [jsonDictionary setValue:@"XYZ" forKey:@"CommandType"]; [jsonDictionary setValue:@"ABC" forKey:@"AppID"]; [jsonDictionary setValue:@"PQM" for

我在字典里有一些数据,比如

 NSMutableDictionary *jsonDictionary;
 jsonDictionary = [[NSMutableDictionary alloc] init];
 [jsonDictionary setValue:@"XYZ" forKey:@"CommandType"];
 [jsonDictionary setValue:@"ABC" forKey:@"AppID"];
 [jsonDictionary setValue:@"PQM" forKey:@"UserName"];

 NSURLResponse *response;
 NSError *error;

 NSData *jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:NSJSONWritingPrettyPrinted error:&error];

 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:
                                [NSURL URLWithString:@"Http://SomeURL"]];

[request setHTTPMethod:@"POST"];
[request setHTTPBody: jsonData];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"];

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *serverResponse = (NSString *)[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

NSMutableDictionary *results = [serverResponse JSONValue];
在我的“结果”中,我得到的是null。。它有什么问题,并在控制台中显示错误。 错误如下

 -JSONRepresentation failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=4 \"Not valid type for JSON\" UserInfo=0x6b8d880 {NSLocalizedDescription=Not valid type for JSON}"

 -JSONFragment failed. Error trace is: (
"Error Domain=org.brautaset.JSON.ErrorDomain Code=1 \"JSON serialisation not supported for NSMutableURLRequest\" UserInfo=0x6b8ddf0 {NSLocalizedDescription=JSON serialisation not supported for NSMutableURLRequest}"

有什么可以帮我解决这个问题。

尝试使用
NSJSONSerialization
执行返回JSON的翻译:

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
id result = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
如果这不起作用,我很确定这是因为您的服务器返回的是空响应。我尝试发送以下请求:

接受:/ 内容类型:application/json 内容长度:65 接受语言:en us 接受编码:gzip,deflate

{ “AppID”:“ABC”, “命令类型”:“XYZ”, “用户名”:“PQM” }

我得到了下面的回复标题(没有人):

HTTP/1.1200ok

日期:2013年1月30日星期三14:58:43 GMT X-AspNet-Version:4.0.30319 内容长度:0 X-Powered-By:ASP.NET 缓存控制:专用 服务器:Microsoft IIS/7.5


内容长度为0。

尝试使用
NSJSONSerialization
执行转换回JSON:

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
id result = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
如果这不起作用,我很确定这是因为您的服务器返回的是空响应。我尝试发送以下请求:

接受:/ 内容类型:application/json 内容长度:65 接受语言:en us 接受编码:gzip,deflate

{ “AppID”:“ABC”, “命令类型”:“XYZ”, “用户名”:“PQM” }

我得到了下面的回复标题(没有人):

HTTP/1.1200ok

日期:2013年1月30日星期三14:58:43 GMT X-AspNet-Version:4.0.30319 内容长度:0 X-Powered-By:ASP.NET 缓存控制:专用 服务器:Microsoft IIS/7.5

内容长度为0。

此行

[request setHTTPBody: temp];
应该是

[request setHTTPBody: jsonData];
这条线

[request setHTTPBody: temp];
应该是

[request setHTTPBody: jsonData];

我认为,这对您很有帮助。因此,您可以尝试以下Code.NSJSON序列化方法。然后尝试以下链接


我认为,这对您很有帮助。因此,您可以尝试以下Code.NSJSON序列化方法。然后尝试以下链接


thnx为您快速回放。但这对我不起作用。。对于如何解决此问题,您还有其他建议吗?您是否检查了从服务器返回的原始数据?可能JSON字符串已损坏。我在浏览器和浏览器上进行了检查,一切正常。我在json检查器中进行了检查,一切都正常。您可以发布资源的路径吗?刚刚对该端点进行了测试调用,得到了一个带有空正文的响应。我添加了code.thnx中显示的所有字段,以便快速重播。但这对我不起作用。。对于如何解决此问题,您还有其他建议吗?您是否检查了从服务器返回的原始数据?可能JSON字符串已损坏。我在浏览器和浏览器上进行了检查,一切正常。我在json检查器中进行了检查,一切都正常。您可以发布资源的路径吗?刚刚对该端点进行了测试调用,得到了一个带有空正文的响应。我添加了代码中显示的所有字段。但是,我键入了错误,抱歉。。我编辑了我的问题我的问题仍然是一样的。。我编辑了我的问题。我的问题仍然是一样的。Thnx用于重播,但我想将数据发布到服务器。。我无法将数据发送到服务器。Thnx用于重播,但我想将数据发送到服务器。。我在向服务器发送数据时遇到问题。