gpt4 book ai didi

ios - JSON 解析错误代码 3840

转载 作者:行者123 更新时间:2023-11-29 03:07:20 25 4
gpt4 key购买 nike

我使用以下方法解析从我的服务器返回的 json 数据:

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"LINK HERE.php"]];

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

NSLog(@"%@", error);
if (json) {
NSArray *quotables = json[@"quotables"];
for (NSDictionary *quotableDict in quotables) {
HonorableMention *quotable = [NSEntityDescription insertNewObjectForEntityForName:@"HonorableMention" inManagedObjectContext:self.user.managedObjectContext];
quotable.body = quotableDict[@"quote"];
quotable.numLikes = quotableDict[@"num_likes"];
quotable.user = tmpUser;
}
NSArray *didYouKnows = json[@"did_you_knows"];
for (NSDictionary *didYouKnowDict in didYouKnows) {
HonorableMention *didYouKnow = [NSEntityDescription insertNewObjectForEntityForName:@"HonorableMention" inManagedObjectContext:self.user.managedObjectContext];
didYouKnow.body = didYouKnowDict[@"question"];
didYouKnow.numLikes = didYouKnowDict[@"num_likes"];
didYouKnow.user = tmpUser;
}
}

数据格式如下,并在jsonlint.com处通过验证:

{
"quotables":[
{
"quote":"\"This is fo real\" by Carlo Abelli, Class III",
"num_likes":"0"
},
{
"quote":"\"We put a lot of effort into this, so this is not going to fail\" by Alex Yu, Class III",
"num_likes":"0"
}
],
"did_you_knows":[
{
"question":"Did you know that you guys better like this idea?",
"num_likes":"0"
},
{
"question":"Did you know that you should listen to the first post?",
"num_likes":"0"
}
]
}

json 序列化失败并出现错误:

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (No string key for value in object around character 1.) UserInfo=0x1093e0050 {NSDebugDescription=No string key for value in object around character 1.}

从服务器返回的数据的NSLog:

<7b71756f 7461626c 65733a5b 7b227175 6f746522 3a225c22 54686973 20697320 666f2072 65616c5c 22206279 20436172 6c6f2041 62656c6c 692c2043 6c617373 20494949 222c226e 756d5f6c 696b6573 223a2230 227d2c7b 2271756f 7465223a 225c2257 65207075 74206120 6c6f7420 6f662065 66666f72 7420696e 746f2074 6869732c 20736f20 74686973 20697320 6e6f7420 676f696e 6720746f 20666169 6c5c2220 62792041 6c657820 59752c20 436c6173 73204949 49222c22 6e756d5f 6c696b65 73223a22 30227d5d 2c646964 5f796f75 5f6b6e6f 77733a5b 7b227175 65737469 6f6e223a 22446964 20796f75 206b6e6f 77207468 61742079 6f752067 75797320 62657474 6572206c 696b6520 74686973 20696465 613f222c 226e756d 5f6c696b 6573223a 2230227d 2c7b2271 75657374 696f6e22 3a224469 6420796f 75206b6e 6f772074 68617420 796f7520 73686f75 6c64206c 69737465 6e20746f 20746865 20666972 73742070 6f73743f 222c226e 756d5f6c 696b6573 223a2230 227d5d7d>

最佳答案

使用您提供的所有代码片段和 Json 数据,一切似乎都运行良好。检查直接来自服务器的具有类似 json 数据的示例:

    NSError *error;
NSURL *url = [NSURL URLWithString:@"https://dl.dropboxusercontent.com/s/ri4vqz7huascopc/json?dl=1&token_hash=AAEaTEDSF-_t_t1Gmp9_WYNsnbShulGSKmIk89FkAUOTsw"];

NSData *data = [NSData dataWithContentsOfURL:url
options:NSDataReadingMappedAlways
error:&error];


NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error:&error];

if (!error) {
NSLog(@"result: %@", dictionary[@"quotables"]);
} else {
NSLog(@"json error: %@", error.localizedDescription);
}

如果该实现不起作用,您的 PHP 可能会发送带有 BOM 的 json。检查这个question更多细节。 BOM 文档 here

关于ios - JSON 解析错误代码 3840,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22620572/

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