java - Parse anonymous array from JSON result with Google HTTP Client Library -



java - Parse anonymous array from JSON result with Google HTTP Client Library -

my rest service respons next data:

[ { "name": "bart", "age": 10 }, { "name": "homer", "age": 42 }, { "name": "marjorie", "age": 34 } ]

the result set array of objects want automatically parse google's http client library. hence created simpsonresult , simpson class:

simpsonresult.java

public class simpsonresult { private list<simpson> simpsons; public simpsonresult() { } public list<simpson> getsimpsons() { homecoming simpsons; } public void setsimpsons(list<simpson> simpsons) { this.simpsons = simpsons; } }

simpson.java

public class simpson { @key private string name; @key private int age; public simpson() { } public string getname() { homecoming name; } public void setname(string name) { this.name = name; } public int getage() { homecoming age; } public void setage(int age) { this.age = age; } }

now want query backend , automatically map response simpsonresult. hence wrote next code:

@override public simpsonresult loaddatafromnetwork() throws ioexception { httprequestfactory requestfactory = gethttprequestfactory(); genericurl url = new genericurl(baseurl); httprequest request = requestfactory.buildgetrequest(url); request.setparser(new jacksonfactory().createjsonobjectparser()); homecoming request.execute().parseas(simpsonresult.class); }

but when execute code, parser exception com.google.api.client.json.jsonparser.

when map string (request.execute().parseasstring()), works.

so think mapping declaration wrong?

the way not valid. simpsonresult, requires field named simpsons, , there no such field in json.

as using generic collection types bit tricky in frameworks, create simple list type within simpson class :

public class simpson { public static class list extends arraylist<simpson> { } ... //you simpson code }

you can utilize simpson.list parse json via annotations.

java json jackson robospice google-http-client

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -