6

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 ?

Bala Krishna
  • 154
  • 2
  • 10
  • 1
    *How do I parse this JSON using Moshi ?* .. as usual ... obviously it's hard to guess what model you need after parsing – Selvin Nov 03 '16 at 14:48

2 Answers2

22

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);
ligi
  • 39,001
  • 44
  • 144
  • 244
Jesse Wilson
  • 39,078
  • 8
  • 121
  • 128
1

In kotlin, Map<String, Object> from json:

val moshi = Moshi.Builder().build()    
val adapter = moshi.adapter<Map<String, Any>>(
            Types.newParameterizedType(Map::class.java, String::class.java, 
Object::class.java)
        )
    val yourMap =  adapter.fromJson(jsonFile)
Muneesh
  • 433
  • 10
  • 19