我在字典里有一些数据,比如
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];在我的"results“中,我得到的是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}"能帮我解决这个问题吗?
发布于 2013-01-30 21:48:18
尝试使用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“}
我得到了以下响应头(没有正文):
超文本传输协议/1.1 200 OK
日期:星期三,2013年1月30日14:58:43 GMT X-AspNet-Version: 4.0.30319 Content-Length: 0 X-Powered-By: ASP.NET缓存控制:专用服务器:微软-IIS/7.5
内容长度为0。
发布于 2013-01-30 21:49:03
这一行
[request setHTTPBody: temp];应该是
[request setHTTPBody: jsonData];发布于 2013-01-30 21:53:14
我认为,这对序列化很有帮助你可以尝试下面的Code.NSJSON you.so method.and尝试这个链接NSJSON Serialization JSON
NSError *error = nil;
NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.1.37:3333/UniECommerce/api/store-list.html"]];
if (jsonData)
{
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if (error)
{
NSLog(@"error is %@", [error localizedDescription]);
// Handle Error and return
return;
}
//NSArray *keys = [jsonObjects allKeys];
NSArray *keys=[[jsonObjects objectForKey:@"storelist"]valueForKey:@"store_list"];
// values in foreach loop
for (NSDictionary *key in keys)// here used to array type key
{
// NSLog(@"%@ is %@",key, [jsonObjects objectForKey:key]);
if ([key valueForKey:@"store_image"]!=[NSNull null])
{
[imageUrls addObject:[key objectForKey:@"store_image"]];
// imageUrls [key valueForKey:@"store_image"];
}
}https://stackoverflow.com/questions/14605448
复制相似问题