缺少AFJSONRequestOperation结果中键的引号?

时间:2012-11-02 17:04:17

标签: objective-c json ios5 afnetworking

整天都陷入困境,并尽快找到帮助解决这个问题。任何帮助都会很棒。谢谢: - )

缺少引号

从网上得到了这个AFJSONRequestOperation请求,它对一个关键错误很有效:一些键缺少那里的引号值=不能使用它们!在AFNetworking背景中有一些令人沮丧的解析,因为这些值按字母顺序返回+某些值上的某些qoutes的“修剪”。但我只是想把webdata“原样”......

格式应如下所示; theKey =“theValue”;

但有时看起来像这样; theKey = theValue; //注意值

周围缺少的引号

网络输出json:

我还检查过我的json格式是否正确来自服务器,输出看起来像这样;

{"results":[{"ID":"3","Row_state":"Visible","CustName":"Customer Name","CustReferens":"Customer Referens","CustReferensMobil":"123456mob","CustNotes":"Customer Notes","Tel":"123456tel","Fax":"123456fax","Web":"www.example.se","Mail":"info@example.se","Street":"The road 5","Zip":"11122","City":"Stockholm","Products":"","Images":"","Logo":"myLogo.jpg","Text":"A lot of text goese here...","Seller":"","Favorite":"YES"},

在应用中,我得到了这个结果;

results =     (
            {
        City = Stockholm;          // Note the missing quotes around the value
        CustName = "Customer Name";
        CustNotes = "Customer Notes";
        CustReferens = "Customer Referens";
        CustReferensMobil = 123456mob;     // Note the missing quotes around the value
        Favorite = YES;             // Note the missing quotes around the value
        Fax = 123456fax;          // Note the missing quotes around the value
        ID = 3;             // Note the missing quotes around the value
        Images = "";
        Logo = "myLogo.jpg";
        Mail = "info@example.se";
        Products = "";
        "Row_state" = Visible;      // Now key got quotes + Note the missing quotes around the value
        Seller = "";
        Street = "The road 5";
        Tel = 123456tel;         // Note the missing quotes around the value
        Text = "A lot of text goese here...";
        Web = "www.example.se";
        Zip = 11122;       // Note the missing quotes around the value
    },

取代此预期结果(所有带键值的引号;)

results =     (
            {
        City = "Stockholm";
        CustName = "Customer Name";
        CustNotes = "Customer Notes";
        CustReferens = "Customer Referens";
        CustReferensMobil = "123456mob";
        Favorite = "YES";
        Fax = "123456fax";
        ID = "3";
        Images = "";
        Logo = "myLogo.jpg";
        Mail = "info@example.se";
        Products = "";
        "Row_state" = Visible;
        Seller = "";
        Street = "The road 5";
        Tel = "123456tel";
        Text = "A lot of text goese here...";
        Web = "www.example.se";
        Zip = "11122";
    },

我的代码;

NSURL *myUrl = [NSURL URLWithString:@"http://example.se/api.lasso"];
NSURLRequest *request = [NSURLRequest requestWithURL:myUrl];
AFJSONRequestOperation *operation;
operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
success:^(NSURLRequest *req, NSHTTPURLResponse *responce, id jsonObject) {
                                            NSLog(@"JSON Responce ÅF: %@",jsonObject);
                                            // Add result (an array from JSON) to NSMutableArray
                                            _restFeed = [jsonObject objectForKey:@"results"];
                                            NSLog(@"RestA-OListTVC > ViewDidload > AFN ÅF: _restFeed count = %i",[_restFeed count]);
                                            // Reload table with new data
                                            [self.tableView reloadData];
                                            }
failure:^(NSURLRequest *req, NSHTTPURLResponse *responce, NSError *error, id jsonObject) {
            NSLog(@"Recieved an HTTP %d", responce.statusCode);
            NSLog(@"The error was: %@",error);
            }];
[operation start];

1 个答案:

答案 0 :(得分:3)

所有JSON键都有引号 - 这是规范的一部分。但是,当转换为NSDictionary时,它们的键会丢失引号,只会成为NSString个对象。

JSON {"a": 1} => Objective-C @{@"a" : @(1)}

NSLog将删除不包含空格的字符串周围的引号。不要使用NSLog的输出作为您要取回的内容的字面表示。只需使用valueForKeyPath:预期的值,它应该都可以正常工作。