gpt4 book ai didi

iphone - JSONRepresentation 显示错误

转载 作者:行者123 更新时间:2023-11-29 13:20:00 25 4
gpt4 key购买 nike

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

 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.. 它有什么问题,并在 consol 中向我显示错误。报错如下。

 -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];

如果这不起作用,我很确定这是因为您的服务器返回空响应。我尝试发送以下请求:

Accept: / Content-Type: application/json Content-Length: 65 Accept-Language: en-us Accept-Encoding: gzip, deflate

{ "AppID": "ABC", "CommandType": "XYZ", "UserName": "PQM" }

我得到了以下响应 header (没有正文):

HTTP/1.1 200 OK

Date: Wed, 30 Jan 2013 14:58:43 GMT X-AspNet-Version: 4.0.30319 Content-Length: 0 X-Powered-By: ASP.NET Cache-Control: private Server: Microsoft-IIS/7.5

内容长度为0。

关于iphone - JSONRepresentation 显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14605448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com