0

I need to parse json form this link https://raw.githubusercontent.com/David-Haim/CountriesToCitiesJSON/master/countriesToCities.json

This file stores the country names as a key, and as city values located in these countries

How to pull the city by the key of the country I know, here is an example with China

NSDictionary *dict = [self JSONFromFile]; 
NSArray *city = [dict objectForKey:@"China"]; 
NSLog(@"Colour name: %@", city);

But I first need to get all the names of countries, which would then give the user the choice of the country for further action.

1
  • 2
    It's not clear what you want but if you're looking to discover all the keys in a dictionary you can use [dict allKeys] Commented Apr 30, 2017 at 10:10

2 Answers 2

1

Fetch the JSON response and store it in a Dictionary and then use the below code to iterate each key value pair in this dictionary.

For Objective C

[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id value, BOOL* stop) {
    NSLog(@"%@ => %@", key, value);
    // This block will iterate for each (key,value) pair in the dictionary.
}];

For Swift 3.0

for (key, value) in dictionary {
    print(key, value)
    // This block will iterate for each (key,value) pair in the dictionary.
}
Sign up to request clarification or add additional context in comments.

Comments

0

If You want to dictionary key (Country) than Simple you can get as this

NSArray* arrayCountyName = [Yourdict allKeys]; // list of your country names

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.