android - How to parse nested array through GSON -


i want parse json response through gson in android, example

 {   "adult": false,   "budget": 63000000,   "spoken_languages": [     {       "iso_639_1": "en",       "name": "english"     }   ], }      

first class this

public detail parsedetailresponse(inputstream json) { gson gson = new gson(); reader reader=new inputstreamreader(json); detail handle = gson.fromjson(reader, detail.class); return handle;  } 

class parsing this

public class detail implements serializable{ private static final long serialversionuid = -6814886315783830255l;  @serializedname("adult") public boolean adult;     @serializedname("spoken_languages") public lang[] languages;    } 

my lang class

public class lang implements serializable{ private static final long serialversionuid = -6814886315783830255l; @serializedname("name") public string name; } 

now want value of lang.name, gives null pointer exception.. pls how getting value...

try this, though it's not elegant way. @ least, things done.

the problem of previous code gson thought spoken_languages in json string array, have create @ array reflected. here choose arraylist. hope problem can settled.

import com.google.gson.gsonbuilder;  import java.io.bytearrayinputstream; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import java.util.list;  public class simpletest {     public static void main(string[] args) {         string json = "{\n" +                 "  \"adult\": false,\n" +                 "  \"budget\": 63000000,\n" +                 "  \"spoken_languages\": [\n" +                 "    {\n" +                 "      \"iso_639_1\": \"en\",\n" +                 "      \"name\": \"english\"\n" +                 "    }\n" +                 "  ]\n" +                 "}     ";          system.out.println(new simpletest().parsedetailresponse(new bytearrayinputstream(json.getbytes())));     }      public detail parsedetailresponse(inputstream json) {         return new gsonbuilder().create().fromjson(new inputstreamreader(json), detail.class);     }      class detail {         private boolean adult;         private long budget;         private arraylist<spokenlanguages> spoken_languages;          public detail() {          }          @override         public string tostring() {             return "dao{" +                     "adult=" + adult +                     ", budget=" + budget +                     ", spoken_languages=" + spoken_languages +                     '}';         }          public boolean isadult() {             return adult;         }          public void setadult(boolean adult) {             this.adult = adult;         }          public long getbudget() {             return budget;         }          public void setbudget(long budget) {             this.budget = budget;         }          public list<spokenlanguages> getspoken_languages() {             return spoken_languages;         }          public void setspoken_languages(arraylist<spokenlanguages> spoken_languages) {             this.spoken_languages = spoken_languages;         }     }      class spokenlanguages {         private string name;         private string iso_639_1;          public string getname() {             return name;         }          public void setname(string name) {             this.name = name;         }          public string getiso_639_1() {             return iso_639_1;         }          public void setiso_639_1(string iso_639_1) {             this.iso_639_1 = iso_639_1;         }          @override         public string tostring() {             return "spokenlanguages{" +                     "name='" + name + '\'' +                     ", iso_639_1='" + iso_639_1 + '\'' +                     '}';         }     } } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -