JSON to java object using gson for multiple records -


i have json file , corresponding java bean classes. json file has multiple records,so have list of objects,where facing problem.list not being populated.  json file: {"requestservices": {`enter code here` "requestservice": [ { "ordernumber":456, "organizationid":123, "headerid":3446 } ] } } java classes: public class requestservices {     private list<requestservice> requestservices=new arraylist<requestservice>();//array of objects      //default getter,setter of list<requestservice> } ----- public class requestservice {        private string ordernumber;     private string organizationid;     private string headerid;     /* setters , getters */} ----- main.class public class jsonbind {     public static void main(string[] args) {     gson gson = new gson();     try {         bufferedreader br = new bufferedreader(new filereader("c:\\json1.json"));         //convert json string object         requestservices obj = gson.fromjson(br, requestservices.class); 

system.out.println(obj.getrequestservices().get(0).getordernumber());//getting error }}} when try print list of objects(i.erequestservice),its giving indexoutofbound in advance, waitin help!

i noticed in json have following hierarchy

requestservices -> requestservice[] 

but in pojos have requestservices -> requestservices try rename pojo or json match properties requestservices

or can map field in pojo annotation @serializedname("requestservices")

for full documentation can check site: gson doc

hope helped!


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -