Home > Enterprise >  JAVA - Obtaining max value from a JSON file
JAVA - Obtaining max value from a JSON file

Time:11-25

I'm a complete beginner in this field and was wondering if someone could giving me a helping hand.

I have the following JSON formatted file which lists countries according to their corona statistics:

https://corona.lmao.ninja/v3/covid-19/countries

Is it possible to obtain the country (preferably top 10) which holds the max amount of cases or deaths?

I have previously used a JSONObject to retrieve the relevant data, but how does one obtain the max value across all countries?

Before, I would store the whole data within a buffer from the following link https://corona.lmao.ninja/v2/all

JSONObject jsonObject = new JSONObject(buffer.toString());

Then retrieve the data as such:

cases = jsonObject.getInt("cases");
totdeath = jsonObject.getInt("deaths");

However, I'm now trying to figure out how I can iterate through https://corona.lmao.ninja/v3/covid-19/countries to obtain the country with the most deaths or cases?

Please review the links above. Any help is appreciated.

CodePudding user response:

How do you eat an elephant?

By cutting it into many pieces.

It's programming. You're doing too many things at once. The proper order is:

{1} How do I turn all this JSON into actual properly structured java objects (answer: Probably GSON or Jackson - libraries that turn JSON into structured data).

{2} How do I sort these on some specific property (answer list.sort(Comparator.comparing(Country::getCases) or similar)

{3} how do I print only the top entries (list.subList(0, 10), probably also needs a range check in case the input has fewer than 10 entries).

CodePudding user response:

The logic I would follow for this is to convert the JSON to an array or arraylist and then sort it by the field that you are interested in.

  • Related