Gson解析复杂的json
2016-05-08 11:42
357 查看
new Thread(){ @Override public void run() { OkHttpClient OkHttpClient = new OkHttpClient(); Request Request = new Request.Builder().url("http://192.168.1.1:8080/json.txt").get().build(); Call calls = OkHttpClient.newCall(Request); try { Response execute = calls.execute(); Gson Gson=new Gson(); GsonBean gsonBean = Gson.fromJson(execute.body().string(), GsonBean.class); System.out.println("name= "+gsonBean.getName()+" Title= "+gsonBean.getStories().get(0).getTitle()); } catch (IOException e) { e.printStackTrace(); } } }.start();
上面有用到okHttp
GsonBean.java
public class GsonBean { private String name; private int timestamp; private List<StoriesBean> stories; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getTimestamp() { return timestamp; } public void setTimestamp(int timestamp) { this.timestamp = timestamp; } public List<StoriesBean> getStories() { return stories; } public void setStories(List<StoriesBean> stories) { this.stories = stories; } public static class StoriesBean { private String date; private String display_date; private int id; private String title; private List<String> images; public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getDisplay_date() { return display_date; } public void setDisplay_date(String display_date) { this.display_date = display_date; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<String> getImages() { return images; } public void setImages(List<String> images) { this.images = images; } } }
json.txt
{
"name": "",
"stories": [
{
"date": "20140428",
"display_date": "4 月 28 日",
"id": 3879106,
"images": [
"http://a/c.jpg"
],
"title": "深夜食堂 · 如何在当代中国活到 70 岁"
},
{
"date": "20140427",
"display_date": "4 月 27 日",
"id": 3876052,
"images": [
"http://a/b.jpg"
],
"title": "深夜食堂 · 不必惶恐"
}
],
"timestamp": 1397052001
}
相关文章推荐
- 使用 Python 处理 JSON 格式的数据
- Gson.toJson()时内存溢出StackOverflowError
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Ruby和Ruby on Rails中解析JSON格式数据的实例教程
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C# JSON格式化转换辅助类 ConvertJson
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法
- php解析json数据实例
- PHP中JSON的应用技巧
- PHP6 先修班 JSON实例代码
- 关于JSON以及JSON在PHP中的应用技巧