java - Gson get list of objects from json -


i have json string contains array of objects(filters array). json string has other objects , fields im interested in parsing json array. can tell me how can using gson?

here json file:

{   name: "test json",    test_ob: {     name: "test"   }     filters[     {     test: 1,     test: 2,     ...     }     ...     ] } 

and code:

 filters filters = new gson().fromjson(jsoncontent.tostring(), filters.class); 

and filters class:

public class filters {      private list<filter> filters;      public list<filter> getfilters() {         return filters;     }      public void setfilters(list<filter> filters) {         this.filters = filters;     } } 

is json valid? try this:

data.json:

{   name: "test json",    test_ob: {     name: "test"   },   filters: [     {         name: filter1,         value: 1     },     {         name: filter2,         value: 2     }   ] } 

filter.java:

public class filter {     private string name;     private string value;      public string getname() {         return name;     }     public void setname(string name) {         this.name = name;     }     public string getvalue() {         return value;     }     public void setvalue(string value) {         this.value = value;     }  } 

data.java:

public class data {     private list<filter> filters;      public list<filter> getfilters() {         return filters;     }      public void setfilters(list<filter> filters) {         this.filters = filters;     } } 

test.java:

public static void main(string[] args) {         gson gson = new gson();          object obj;         try {             jsonparser parser = new jsonparser();             obj = parser.parse(new filereader("c:\\data.json"));             jsonobject jsonobject = (jsonobject) obj;              data data = gson.fromjson(jsonobject, data.class);          } catch (jsonioexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (jsonsyntaxexception e) {             // todo auto-generated catch block             e.printstacktrace();         } catch (filenotfoundexception e) {             // todo auto-generated catch block             e.printstacktrace();         }      } 

Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -

Nuget pack csproj using nuspec -