Question: JAVA / JSON HELP: I have the following JSON data, that has an array of 'worldCountries'. Bordering in the JSON data indicates which countries are
JAVA / JSON HELP:
I have the following JSON data, that has an array of 'worldCountries'. "Bordering" in the JSON data indicates which countries are being bordered by another country or not, ie. Turkey is bordering Greece, Greece is bordering Turkey, Australia has no countries bordering it.
In JAVA, how can I list the bordering countries for each country and answer the question whether two countries are bordering or not?
"worldCountries" : [{ "id" : 1, "name" : "Turkey", "population" : 79000000, "cities" : [{ "id" : 11, "name" : "Ankara", "population": 5000000 },{ "id" : 12, "name" : "Istanbul", "population": 15000000 }] },{ "id" : 2, "name" : "Australia", "population" : 24000000, "cities" : [{ "id" : 21, "name" : "Perth", "population": 2000000 },{ "id" : 22, "name" : "Brisbane", "population": 2000000 }] },{ "id" : 3, "name" : "Greece", "population" : 10000000, "cities" : [{ "id" : 31, "name" : "Athens", "population": 660000 },{ "id" : 32, "name" : "Patras", "population": 170000 }] }],
"bordering" : { "1" : [3], "2" : [ ], "3" : [1] } }
I have offered psuedocode in what way I think to approach it but not sure how to undergo in JAVA.
//List the bordering countries for each country//
// public void listCountries() { // Loop through each country in the JSON under "worldCountries" // First print the current country's name // Find the country id // Look in the JSON under "bordering" for that country's id // Extract the list of bording countries associated with the current country, and loop through them // Use the id's listed in the current country's "bordering" section to find and print the name of it's bordering countries }
//Answer whether two countries are bordering or not//
// public boolean checkBorders(String country1, String country2) { // Find the id of country1 in the "worldCountries" part of the JSON // Find the id of country2 in the "worldCountries" part of the JSON // Check country1's border countries in the "bordering" section of the JSON // If country2 id is in country1 id's bordering //return True // Else //return False
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
