gson的使用
Android中有的时候通过第三方API接口获取json格式的数据,那么如何解析json格式的数据呢?
其实我们可以使用gson.jar来帮我们完成这项工作。
比如一个json的格式如下:
{"xingming":"wyl","sex":"male","hobby":"zhihu"},
如果我们用gson来帮我们解析,那么我们就需要编写一个javabean,而这个bean的属性就是json的属性。
具体实例如下:
package wyl; import java.util.ArrayList; /** * 用于gson解析 * @author Administrator * */ public class ReturnMsg { String code; String text; public static ArrayList list; public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getText() { return text; } public void setText(String text) { this.text = text; } public static ArrayList getList() { return list; } public static void setList(ArrayList list) { ReturnMsg.list = list; } }
测试类:
package wyl; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import com.google.gson.Gson; public class gsonTest { public static void main(String[] args) throws IOException { String key = "e59219b3f450256c2a3da7157ae651f2"; String info = "上海到成都的火车";//问题 // String info = "我爱你";//问题 info = URLEncoder.encode(info, "UTF-8"); String url = "http://www.tuling123.com/openapi/api?key="+key+"&info="+info; URL u = new URL(url); URLConnection conn = u.openConnection(); conn.connect(); InputStream ips = conn.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader( ips, "utf-8")); StringBuffer sb = new StringBuffer(); String line = ""; while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); System.out.println("sb:"+sb); String sb2 = sb.toString(); Gson gson = new Gson();//实例化一个gson对象 //gson.fromJson(paraA,paraB);把json对象转化为java对象, //paraA是String类型的json字符串,paraB是这个json对应Bean的类类型(java反射中有) ReturnMsg rtn = gson.fromJson(sb2, ReturnMsg.class);//sb:{"code":305000,"text":"亲,已帮您找到列车信息","list":[{"trainnum":"D636/D637(动车组)","start":"上海虹桥","terminal":"成都东","starttime":"06:09","endtime":"21:15","icon":"http://unidust.cn/images/api-train.jpg","detailurl":"http://touch.qunar.com/h5/train/"},{"trainnum":"D2206/D2207(动车组)","start":"上海虹桥","terminal":"成都东","starttime":"06:39","endtime":"21:47","icon":"http://unidust.cn/images/api-train.jpg","detailurl":"http://touch.qunar.com/h5/train/"},{"trainnum":"K290/K291(快速)","start":"上海","terminal":"成都","starttime":"08:43","endtime":"21:33(+1)","icon":"http://unidust.cn/images/api-train.jpg","detailurl":"http://touch.qunar.com/h5/train/"},{"trainnum":"K1156/K1157(快速)","start":"上海","terminal":"成都","starttime":"09:16","endtime":"16:18(+1)","icon":"http://unidust.cn/images/api-train.jpg","detailurl":"http://touch.qunar.com/h5/train/"},{"trainnum":"K351/K354(快速)","start":"上海南","terminal":"成都","starttime":"17:34","endtime":"04:07(+2)","icon":"http://unidust.cn/images/api-train.jpg","detailurl":"http://touch.qunar.com/h5/train/"},{"trainnum":"K282/K283(快速)","start":"上海","terminal":"成都","starttime":"20:40","endtime":"11:30(+2)","icon":"http://unidust.cn/images/api-train.jpg","detailurl":"http://touch.qunar.com/h5/train/"}]} System.out.println("code:"+rtn.getCode());//code:305000 System.out.println("text:"+rtn.getText());//text:亲,已帮您找到列车信息 } }