1

I am working with Rxandroid and retrofit. I have a json with dynamically changing array name like this,

{
  "2016-10-02": [
    {
      "name": "foo",
      "id": "1",
      "category": "bar"

    },
    {
     "name": "foo",
      "id": "2",
      "category": "bar"
    },
    {
     "name": "foo",
      "id": "3",
      "category": "bar"
    },
    {
      "name": "foo",
      "id": "4",
      "category": "bar"
    }
  ],
  "2016-10-01": [
    {
      "name": "foo",
      "id": "5",
      "category": "bar"
    },
    {
      "name": "foo",
      "id": "6",
      "category": "bar"
    },
  ],
 "2016-10-03": [
    {
      "name": "foo",
      "id": "5",
      "category": "bar"
    }
  ]
}

The date key name for each array changes automatically and the number of array changes. In this example there are 3 arrays with date key. But the number of these array varies.

I have been through various links in stackoverflow but could not solve the issue.

Suresh Basnet
  • 147
  • 2
  • 13
  • You can go 2 ways. Implement your own deserializer or tell Retrofit that you want JsonObject(GSON library) as response and get the inner arrays from that object. – loshkin Sep 29 '16 at 08:32
  • Check [this answer](http://stackoverflow.com/questions/7304002/how-to-parse-a-dynamic-json-key-in-a-nested-json-result) – Burak Cakir Sep 29 '16 at 08:40
  • @Suresh Basnet : Check the below answer with complete parsing for your json response – Jai Sep 29 '16 at 08:52
  • @Suresh Basnet : Is it done? – Jai Sep 29 '16 at 11:12
  • @jai I have copied my response string and each time it shows the values form the response string not from the server. we cannot directly set Name,Id and Category for mCategory. It should first set the array with dynamic key. – Suresh Basnet Sep 30 '16 at 05:54
  • @Jai thanks.. forgot to replace the callback with responseBody – Suresh Basnet Sep 30 '16 at 07:10

2 Answers2

2

Use JSONObject keys() to get the key and then you could iterate each key to get the dynamic values :

    JSONObject object = new JSONObject("your response string")
    Iterator keys = object.keys();

    //Let's consider your POJO class is CategoryClass
    // Let's take HashMap to store your POJO class for specific KEY
    HashMap<String, ArrayList<CategoryClass>> mMap = new HashMap<String, ArrayList<CategoryClass>>();

    while(keys.hasNext()) {
        // here you will get dynamic keys
        String dynamicKey = (String)keys.next();

        // get the value of the dynamic key
        JSONArray dynamicValue = object.getJSONArray(currentDynamicKey);

        //Let's store into POJO Class and Prepare HashMap.
         ArrayList<CategoryClass> mCategoryList = new ArrayList<CategoryClass>();
         for(int i = 0 ; i < dynamicValue.length(); i++){

             CategoryClass mCategory = new CategoryClass();
             mCategory.setName(dynamicValue.getString("name"));
             mCategory.setId(dynamicValue.getString("id"));
             mCategory.setCategory(dynamicValue.getString("category"));

             mCategoryList.add(mCategory);


         }
        //Add Into Hashmap
        mMap.put(dynamicKey, mCategoryList);

    }
Jai
  • 1,974
  • 2
  • 22
  • 42
1

From my point of view,this format is not recommended.The date should be value such as "date":"2016-10-01" instead of json key.

Qian Sijianhao
  • 564
  • 7
  • 19