0

when i try parse the local json file it returns null value am using xcode6.2 beta and ios 8.1

I have followed this posts also How to read a JSON file using the NSJSONSerialization Class Reference?Cocoa-Touch - How parse local Json file2

My code is

    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";
}
10
  • Is jsonPath is nil or not?? Commented Dec 12, 2014 at 7:17
  • json path is not nill, only after serialization it returns null value Commented Dec 12, 2014 at 7:24
  • 1
    Your JSON is invalid. Commented Dec 12, 2014 at 7:25
  • @satheesh for future reference check your json in json.parser.online.fr Commented Dec 12, 2014 at 7:27
  • { "AD": "Andorra", "AE": "United Arab Emirates", "AF": "Afghanistan", "AG": "Antigua and Barbuda", "AI": "Anguilla", "AL": "Albania", "AM": "Armenia" } I have add this json value too but it reurns null value Commented Dec 12, 2014 at 7:28

2 Answers 2

1

Your json file is corrupted. Replace your json file with this and it will work

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

To validate your JSON file you can go for this link

Sign up to request clarification or add additional context in comments.

2 Comments

@SatheeshKumar I have checked it, it is working fine please try to log your myJSON string. may be you are not able to read the file.
thanks for ur support, i have formatted my json value, now its working fine
0

Your Json is not valid. Check your Json here

You have put following Json in your JsonFile.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.