java - Parsing JSON using GSON -
this continuation of previous question. after getting json request , turning json, trying parse result java object.
public class netclientget { @suppresswarnings("unchecked") public static void main(string[] args) { string urlyesterday = "http://fids.changiairport.com/webfids/fidsp/get_flightinfo_cache.php?d=-1&type=pa&lang=en"; string yesterdayjson = getdatafromweb(urlyesterday); //system.out.println("yesterdayjson : " + yesterdayjson); //jsonarray.add(yesterdayjson); gson gson = new gson(); list<flightinfo> flightarray = (list<flightinfo>) gson.fromjson(yesterdayjson, flightinfo.class); system.out.println("flightarray : " + flightarray); } private static string getdatafromweb(string targeturl) { try { url url = new url(targeturl); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setrequestmethod("get"); conn.setrequestproperty("accept", "application/json"); if (conn.getresponsecode() != 200) { throw new runtimeexception("failed : http error code : " + conn.getresponsecode()); } bufferedreader br; if ("gzip".equalsignorecase(conn.getcontentencoding())) { br = new bufferedreader(new inputstreamreader( (new gzipinputstream(conn.getinputstream())))); } else { br = new bufferedreader(new inputstreamreader( (conn.getinputstream()))); } // retrieve data server string output = null; final stringbuffer buffer = new stringbuffer(16384); while ((output = br.readline()) != null) { buffer.append(output); } conn.disconnect(); // extract json jsonp envelope string jsonp = buffer.tostring(); string json = jsonp.substring(jsonp.indexof("(") + 1, //this index of callback envelope jsonp.lastindexof(")")); //system.out.println("output server"); //system.out.println(json); return json; } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } return null; } }
i tried:
gson gson = new gson(); flightinfo flightarray = gson.fromjson(yesterdayjson, flightinfo.class);
but got me null, sure need parse list since data has lot of entries.
sample of json
"flights": [ { "date": "2013-05-12", "scheduled_time": "23:30", "estimated_time": "23:09*", "airline": "tr", "flight_no": "tr2109", "airport": "bkk", "origin": "bangkok (suvarnabhumi)", "via": "", "terminal": "2", "belt": "35", "status": "landed", "airline_name": "tiger airways", "airline_alias": "", "unixtime": "1368372600", "master_flight_no": "tr2109", "slave_flight_no": [] } ]
and flightinfo.class
import java.sql.time; import java.util.date; public class flightinfo { private date date; private time scheduled_time; private time estimated_time; private string airline; private string flight_no; private string airport; private string origin; private string via; private string terminal; private string belt; private string status; private string airline_name; public date getdate() { return date; } public void setdate(date date) { this.date = date; } public time getscheduled_time() { return scheduled_time; } public void setscheduled_time(time scheduled_time) { this.scheduled_time = scheduled_time; } public time getestimated_time() { return estimated_time; } public void setestimated_time(time estimated_time) { this.estimated_time = estimated_time; } public string getairline() { return airline; } public void setairline(string airline) { this.airline = airline; } public string getflight_no() { return flight_no; } public void setflight_no(string flight_no) { this.flight_no = flight_no; } public string getairport() { return airport; } public void setairport(string airport) { this.airport = airport; } public string getorigin() { return origin; } public void setorigin(string origin) { this.origin = origin; } public string getvia() { return via; } public void setvia(string via) { this.via = via; } public string getterminal() { return terminal; } public void setterminal(string terminal) { this.terminal = terminal; } public string getbelt() { return belt; } public void setbelt(string belt) { this.belt = belt; } public string getstatus() { return status; } public void setstatus(string status) { this.status = status; } public string getairline_name() { return airline_name; } public void setairline_name(string airline_name) { this.airline_name = airline_name; } @override public string tostring() { return "flightinfo [date=" + date + ", scheduled_time=" + scheduled_time + ", estimated_time=" + estimated_time + ", airline=" + airline + ", flight_no=" + flight_no + ", airport=" + airport + ", origin=" + origin + ", via=" + via + ", terminal=" + terminal + ", belt=" + belt + ", status=" + status + ", airline_name=" + airline_name + "]"; } }
i assume json response surrounded { }
, otherwise it's not valid json...
that said, need class parse json response, example:
public class response { private list<flightinfo> flights; //getters & setters }
now can parse response with:
gson gson = new gson(); response response = gson.fromjson(yesterdayjson, response .class); list<flightinfo> flightarray = response.getflights();
note: looking @ class flightinfo
, json response, think you'll have trouble parsing date
, time
fields, suggest parse them strings , correct transformation... or you'll have create custom deserializer.
Comments
Post a Comment