gpt4 book ai didi

objective-c - AFJSONRequestOperation 结果中的键周围缺少引号?

转载 作者:行者123 更新时间:2023-12-04 05:26:04 26 4
gpt4 key购买 nike

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

缺少的引号

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

格式应该是这样的;
theKey = "theValue";

但有时看起来像这样;
键 = 值;//注意值周围缺少的引号

Web 输出 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];

最佳答案

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

JSON {"a": 1} => Objective-C @{@"a" : @(1)}NSLog将去除不包含空格的字符串周围的引号。不要使用 NSLog 的输出作为您返回的字面表示。只需按预期使用值,使用 valueForKeyPath:它应该一切正常。

关于objective-c - AFJSONRequestOperation 结果中的键周围缺少引号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13199902/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com