繁体   English   中英

JSONRepresentation显示错误

[英]JSONRepresentation showing error

我在字典中有一些数据,例如

 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接受语言:zh-cn接受编码:gzip,deflate

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

然后,我得到了以下响应头(没有主体):

HTTP / 1.1 200 OK

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

内容长度为0。

这条线

[request setHTTPBody: temp];

应该

[request setHTTPBody: jsonData];

我认为,这对您有所帮助。因此,您可以尝试以下Code.NSJSON序列化方法,然后尝试此链接NSJSON序列化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"];
            }
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM