JSONRepresentation显示错误

时间:2013-01-30 13:41:09

标签: 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" 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}"

可以帮助我解决这个问题。

3 个答案:

答案 0 :(得分:2)

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

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
id result = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];

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

  

接受: /       Content-Type:application / json       内容长度:65       接受语言:en-us       Accept-Encoding:gzip,deflate

     

{         “AppID”:“ABC”,         “CommandType”:“XYZ”,         “UserName”:“PQM”       }

我收回了以下响应标题(没有正文):

  

HTTP / 1.1 200确定

     

日期:2013年1月30日星期三格林尼治标准时间14:58:43       X-AspNet-Version:4.0.30319       内容长度:0       X-Powered-By:ASP.NET       缓存控制:私有       服务器:Microsoft-IIS / 7.5

内容长度为0.

答案 1 :(得分:0)

这一行

[request setHTTPBody: temp];

应该是

[request setHTTPBody: jsonData];

答案 2 :(得分:-1)

我认为,它对您有帮助。您可以按照Code.NSJSON序列化方法尝试此操作。并尝试此链接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"];
            }
        }