1

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

 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}"

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

4

3 回答 3

2

尝试使用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”,“用户名”:“PQM”}

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

HTTP/1.1 200 正常

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

内容长度为 0。

于 2013-01-30T13:48:17.893 回答
0

这条线

[request setHTTPBody: temp];

应该

[request setHTTPBody: jsonData];
于 2013-01-30T13:49:03.227 回答
-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"];
            }
        }
于 2013-01-30T13:53:14.073 回答