1

My json response is like

"answers": [{
"data": [
  {
    "currency": "EUR/JPY",
    "rate": "122.593",
    "timestamp": "1497524449141"
  },
  {
    "currency": "EUR/CHF",
    "rate": "1.08779",
    "timestamp": "1497524449234"
  },
  {
    "currency": "USD/CAD",
    "rate": "1.32772",
    "timestamp": "1497524449235"
  },
  {
    "currency": "AUD/USD",
    "rate": "0.75875",
    "timestamp": "1497524449148"
  },
  {
    "currency": "GBP/JPY",
    "rate": "140.248",
    "timestamp": "1497524449230"
  }
],
"metadata": {"count": 60},
"actions": [{
  "type": "table",
  "columns": {
    "currency": "Valuta",
    "rate": "Quota"
  },
  "count": -1
}]
}],

Here action Json array has json object columns. Key currency and rate name is dynamic i.e it may have other name like game and goal or anything else and also number of key may also change. Accordingly key in data json array also change. Data json array use same key name.

halfer
  • 19,824
  • 17
  • 99
  • 186
Amit Tiwary
  • 787
  • 1
  • 8
  • 16
  • 1
    Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Jun 15 '17 at 13:25

1 Answers1

0

You could use Gson library and parse data object as Array of HashMaps.

Updated: Here is a small code example (I simplefied your JSON a bit): ­

private void parse() {        
    String json = "[{\"data\":[{\"currency\":\"EUR/JPY\",\"rate\":\"122.593\",\"timestamp\":\"1497524449141\"},{\"currency\":\"EUR/CHF\",\"rate\":\"1.08779\",\"timestamp\":\"1497524449234\"},{\"currency\":\"USD/CAD\",\"rate\":\"1.32772\",\"timestamp\":\"1497524449235\"},{\"currency\":\"AUD/USD\",\"rate\":\"0.75875\",\"timestamp\":\"1497524449148\"},{\"currency\":\"GBP/JPY\",\"rate\":\"140.248\",\"timestamp\":\"1497524449230\"}],\"metadata\":{\"count\":60},\"actions\":[{\"type\":\"table\",\"columns\":{\"currency\":\"Valuta\",\"rate\":\"Quota\"},\"count\":-1}]}]";
    Type type = new TypeToken<List<DataObject>>(){}.getType();
    List<DataObject> list = new Gson().fromJson(json, type);
    for (DataObject object : list) {
        for (HashMap<String, String> map : object.data) {
            Log.e("mapItemStart", "===============");
            for (String item : map.keySet()) {
                Log.e("mapItem", item + " -> " + map.get(item));
            }
            Log.e("mapItemEnd", "===============");
        }
    }
}

public static class DataObject {
    public List<HashMap<String, String>> data;
}
kaplis
  • 63
  • 7