gpt4 book ai didi

ios - json序列化在序列化本地json文件时返回null值

转载 作者:行者123 更新时间:2023-11-29 02:26:41 28 4
gpt4 key购买 nike

当我尝试解析本地 json 文件时,它返回 null 值我正在使用 xcode6.2 beta 和 ios 8.1

我也关注了这个帖子 How to read a JSON file using the NSJSONSerialization Class Reference? Cocoa-Touch - How parse local Json file 2

我的代码是

    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"countries"
ofType:@"json"];
NSError *error = nil;
NSString *myJSON = [[NSString alloc] initWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:NULL];
id json = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding]
options:NSJSONReadingMutableContainers
error:&error];
NSLog(@"JSON: %@", json);

//Upto myJson string value the json file converted into string but the jsonserialization only have //the problem and the response is always null.
//please give some suggestions,
//Thanks in advance,
my json file contains this values

{
"AD" = "Andorra";
"AE" = "United Arab Emirates";
"AF" = "Afghanistan";
"AG" = "Antigua and Barbuda";
"AI" = "Anguilla";
"AL" = "Albania";
"AM" = "Armenia";
}

最佳答案

您的 json 文件已损坏。用这个替换你的 json 文件,它会工作

{
"AD": "Andorra",
"AE": "United Arab Emirates",
"AF": "Afghanistan",
"AG": "Antigua and Barbuda",
"AI": "Anguilla",
"AL": "Albania",
"AM": "Armenia"
}

要验证您的 JSON 文件,您可以使用 this link

关于ios - json序列化在序列化本地json文件时返回null值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27438573/

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