-1

I have an JSON response from the retrofit which look's like this:

  {
  "rates": {
    "CAD": 1.5299,
    "KRW": 1332.82,
    "MYR": 4.7006
  },
  "base": "EUR",
  "date": "2020-04-03"
}

And I wondering, How I may store some of that response In the Map? Could you provide me any hint, please?

I would like to have this data in map

    "CAD": 1.5299,
    "KRW": 1332.82,
    "MYR": 4.7006

Do I need to add this manually, or there's some faster way?

qwesd
  • 3
  • 2
  • Does this answer your question? [Convert JSONObject to Map](https://stackoverflow.com/questions/21544973/convert-jsonobject-to-map) – Siarhei Apr 04 '20 at 12:03
  • Hmm, not exactly. I'll post an answear to question to explain you my "requirements" – qwesd Apr 04 '20 at 12:50

1 Answers1

0

Try this:

Map<String, Double> map = new HashMap<>();
JSONObject json = new JSONObject("your json");
try {
    JSONObject rates = json.getJSONObject("rates");
    Iterator<String> cur = rates.keys();
    while(cur.hasNext()){
        map.put(cur.next(), rates.getDouble(cur.next()));
    }
} catch (JSONException e) {
    e.printStackTrace();
}