序列化本地json文件时,Json序列化返回null值

时间:2014-12-12 07:07:23

标签: ios json

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

我也关注了这些帖子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";
}

2 个答案:

答案 0 :(得分:1)

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

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

要验证您的JSON文件,您可以转到this link

答案 1 :(得分:0)

您的Json无效。 Check your Json here

您已将Json放在JsonFile

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