Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse unknown key jsons using Moshi

This is the example of JSON I am having

filter : [

    { category: [] } ,

    { sub-category: [] } ,

    { brand: [] } ,

    { color: [] } 
   ]

Note that the labels "category,subcategory..." may vary dynamically

How do I parse this JSON using Moshi ?

like image 449
Bala Krishna Avatar asked Dec 01 '22 15:12

Bala Krishna


1 Answers

Decode it as a Map<String, Object>. The map keys will be your JSON’s values. You can get that adapter like so:

Type map = Types.newParameterizedType(Map.class, String.class, Object.class);
JsonAdapter<Map<String, Object>> adapter = moshi.adapter(map);
like image 143
Jesse Wilson Avatar answered Dec 04 '22 12:12

Jesse Wilson