【问题标题】:Missing quotes around keys in AFJSONRequestOperation result?AFJSONRequestOperation 结果中的键周围缺少引号?
【发布时间】:2012-11-02 17:04:16
【问题描述】:

整天被这个问题困扰,并寻求帮助尽快解决这个问题。任何帮助都会很棒。谢谢:-)

缺少的引号

从网络上获得了这个 AFJSONRequestOperation 请求,它工作得很好,除了一个关键的错误:一些键缺少那里的引号值 = 不能使用它们!显然,AFNetworking 背景中正在进行一些解析,因为这些值按字母顺序返回 + 某些值上的一些 qoutes 的“修剪”。但我只想要“原样”的网络数据......

格式应该是这样的; 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];

【问题讨论】:

    标签: objective-c json ios5 afnetworking


    【解决方案1】:

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

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

    NSLog 将去掉不包含空​​格的字符串周围的引号。不要使用NSLog 的输出作为你得到的内容的文字表示。只需按预期使用值,使用valueForKeyPath:,它应该可以正常工作。

    【讨论】:

    • 谢谢马特。这解决了问题 :-) 提示:也许您应该在 AFN Wiki 上发布一些关于此的内容,以防其他人需要它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多