java - Dynamic Json Parsing Using Gson Android -


i have json mentioned below.

{   "product": [     {       "classification": "abc",       "abc": [         {           "classification": "abc",           "name": "abc new product one",           "price": "10775.0000",         },         {           "classification": "abc",           "name": "abc new product two",           "price": "12725.0000",         }       ]     },     {       "classification": "def",       "def": [         {           "classification": "def",           "name": "def product one",           "price": "728.0000",         },         {           "classification": "def",           "name": "def product two",           "price": "1263.0000",         },       ]     }   ],   "status": "ok",   "message": "success" } 

in above json, key in capital letter dynamic

(ex: abc, def)

. i've created pojo class below:

    public class productresponse{         private string status;        private string message;        private list<products>;         getters , setters     }      public class products{         private string classification;      } 

i'm struggling write next part in products pojo class, keys in capitals

(ex: abc, def)

are dynamic. using volley library getting data , parsing i'm using gson library. please me out.

you can't have dynamic name. reason being name in json needs linked attribute in object. recommend add "classification" attribute in json , model following :

{   "type": "def",   " results": [     {       "name": "def product one",       "price": "728.0000",     },     {       "name": "def product two",       "price": "1263.0000",     },   ] }    public final class productresponse{         private string status;        private string message;        private list<products> product = new arraylist<>();         // getters , setters     }      public final class products{         private string type; // type of product // abc or def        private list<result> results = new arraylist<>();         // getters , setters     }        public final class result{         private string name;        private string price;        // getters , setters     } 

your products class has list of results associated classification !

hope helps !


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -