-1

I am trying to get an array with a kist of countries from a JSON file. I found 2 JSON files online of the formats below. I can load into an NSDictionary, my question is how to extract and format. These don't have a key to extract by. I am guessing I need a for loop but not sure how to do.

[ 
  {name: 'Afghanistan', code: 'AF'}, 
  {name: 'Ã…land Islands', code: 'AX'}, 
  {name: 'Albania', code: 'AL'}, 

and

[
    {
    "name": "Afghanistan",
    "nativeName": "\u0627\u0641\u063a\u0627\u0646\u0633\u062a\u0627\u0646",
    "tld": [".af"],
    "cca2": "AF",
    "ccn3": "004",
    "cca3": "AFG",
    "currency": ["AFN"],
    "callingCode": ["93"],
    "capital": "Kabul",
    "altSpellings": ["AF", "Af\u0121\u0101nist\u0101n"],
    "relevance": "0",
    "region": "Asia",
    "subregion": "Southern Asia",
    "language": ["Pashto", "Dari"],
    "languageCodes": ["ps", "uz", "tk"],
    "translations": {
        "cy": "Affganistan",
        "de": "Afghanistan",
        "es": "Afganist\u00e1n",
        "fr": "Afghanistan",
        "it": "Afghanistan",
        "ja": "\u30a2\u30d5\u30ac\u30cb\u30b9\u30bf\u30f3",
        "nl": "Afghanistan",
        "hr": "Afganistan"
    },
    "latlng": [33, 65],
    "demonym": "Afghan",
    "borders": ["IRN", "PAK", "TKM", "UZB", "TJK", "CHN"],
    "area": 652230
},
{
    "name": "\u00c5land Islands",
    "nativeName": "\u00c5land",
    "tld": [".ax"],
    "cca2": "AX",
    "ccn3": "248",

My code to extract dictionary (from first json file)

+ (NSDictionary *)getCountryList
{
    NSURL *myURL = [[NSURL alloc]initWithString:@"http://www.osmi-tech.com/iphone/json/countriessimp.json"];
    NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];
    NSError *error = nil;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
    return json;

}
Ralph
  • 9
  • 2
  • the top level of the json is an array, are you sure you can add this to a dictionary? :/ –  May 18 '14 at 19:04
  • Change `NSDictionary *json = ..` to `NSArray *json = ..`, to match the actual JSON, then work from there. You do know how to index an array, right? – Hot Licks May 18 '14 at 19:10
  • possible duplicate of [How to use NSJSONSerialization](http://stackoverflow.com/questions/8356842/how-to-use-nsjsonserialization) –  May 18 '14 at 19:34

2 Answers2

0

-The data from the link you are using in the given code snippet is not a valid JSON, so NSJSONSerialization will not be able to parse.

-The data is in the format of JSON Array, so parsing it will return an NSArray not a NSDictionary. I have modified your code snippet with correct link, hope it will help:

+ (NSArray *)getCountryList
{
    NSURL *myURL = [[NSURL alloc]initWithString:@"http://www.osmi-tech.com/iphone/json/countries.json"];
    NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];

    NSError *error = nil;
    NSArray *json = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];

    return json;

}
Rajeev
  • 585
  • 3
  • 13
0

Thanks Rajeev. My final code to extract a list of countries into an NSArray.

+ (NSArray *)getCountryList
{
NSURL *myURL = [[NSURL alloc]initWithString:@"http://www.osmi-tech.com/iphone/json/countries.json"];
NSData *myData = [[NSData alloc]initWithContentsOfURL:myURL];

NSError *error = nil;
NSArray *json = [NSJSONSerialization JSONObjectWithData:myData options:NSJSONReadingMutableContainers error:&error];
NSArray *countryList = [json valueforkey:@"name"];


return countryList;
}
Ralph
  • 9
  • 2