-1

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!

Felix
  • 11
  • 1
  • 3
    Does this answer your question? [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Tim Hunter Oct 29 '20 at 20:21
  • 1
    *That* is not valid JSON, so it's simply amazing that it works at all in Python. – Andreas Oct 29 '20 at 20:25

1 Answers1

1

Since your json string is actually an array of objects, you need to construct a JSONArray, not a JSONObject. From there, you should be able to get the first element and then the field from that element:

JSONArray arr = new JSONArray(jsonAsString);
Double val = arr.getJSONObject(0).getDouble("cases_per_population");
Chris
  • 22,923
  • 4
  • 56
  • 50
  • That would work if the JSON was actually an array, but since it doesn't start with a `[`, that's just bound to throw an exception. – Andreas Oct 29 '20 at 20:26
  • Since Felix said he could parse it in python, I assume he has valid json and that the missing [ ] was just a copy/paste error. – Chris Oct 29 '20 at 20:29
  • Hi Chris and Andreas, thank you for your help. I acutally deleted the [ ] manually to be able to read it into a JSONObject. It threw an exception before I did this. However I could not continue from here because I should have - as you correctly explained - used a JSONArray. Thank you guys. Chris, I had to add the word "new" to your code in the first line after the equal sign. Maybe you can edit this for other users. Best regards, Felix – Felix Oct 29 '20 at 22:17