I receive a JSON file back from a HTTP get request as a String. I need to access a specific entry by going to the first block (index 0) and then get the value for a specific key.
The JSON looks like this:
{
"fb_id": "e0a8495a0a4b0a183527f9aa5cf9389b",
"location_label": "Halle (Saale)",
"cases": 782,
"relative_case_changes": 7.0,
"cases_per_population": 0.0032684519,
"cases_per_100k": 326.8452,
"deaths": 14,
"relative_death_changes": 0.0,
"deaths_per_population": 5.85145e-05,
"deaths_per_100k": 5.8514,
"population": 239257,
"bundesland_name": "Sachsen-Anhalt",
"bundesland_ags": "15",
"kreis_name": "Halle (Saale)",
"kreis_ags": "15002",
"kreis_nuts": "DEE02",
"publication_datetime": "2020-10-29T00:00:00.000Z",
"fb_datetime": "2020-10-29T03:30:24.000Z"
}, {
"fb_id": "f10a78ec290ee09d249bd5aa7020c4c6",
"location_label": "Halle (Saale)",
"cases": 775,
"relative_case_changes": 0.0,
"cases_per_population": 0.0032391947,
"cases_per_100k": 323.9195,
"deaths": 14,
"relative_death_changes": 0.0,
"deaths_per_population": 5.85145e-05,
"deaths_per_100k": 5.8514,
"population": 239257,
"bundesland_name": "Sachsen-Anhalt",
"bundesland_ags": "15",
"kreis_name": "Halle (Saale)",
"kreis_ags": "15002",
"kreis_nuts": "DEE02",
"publication_datetime": "2020-10-28T00:00:00.000Z",
"fb_datetime": "2020-10-28T17:22:13.000Z"
} (...)
In Python I have solved my problem in minutes:
jsonFile = json.loads(jsonAsString)
print(jsonFile[0]["cases_per_population"])
==> 0.0032684519
However I need it in Java and have been struggling for hours. The only thing I have so far is this:
JSONObject jsonObject = new JSONObject(jsonAsString);
Can anybody help me? Thank you!