一、Json解析
Gradle文件倒入依赖:
compile 'com.google.code.gson:gson:2.8.2' compile 'com.alibaba:fastjson:1.2.46' compile 'com.alibaba:fastjson:1.1.68.android'
布局文件:
xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mrzhao.day6jsonproject.MainActivity">android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> ScrollView>android:id="@+id/showTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:textSize="20sp" />
Activity:
/** * Json 解析 ** 什么是Json? * Json是一种轻量级的 数据交换格式 。 Json数据的本质 就是一个字符串 字符串内的数据是 Json格式的 *
* 对象表示为键值对 * 数据由逗号分隔 * 花括号保存对象 * 方括号保存数组 *
* Json的解析方式: *
* 1。Json 表示 的是 使用原生的方式进行数据解析 JSONObject 解析对象 JSONArray 解析数组 * 2。Gson 表示 使用 Json解析工具进行解析 Gson 是 Google 提供的 * 3。FastJson 表示 使用 Json解析工具进行解析 FastJson 是 alibaba 提供的 *
* GsonFormat 一个AndroidStudio 插件 作用是 能够 根据Json数据 生成实体类 * * * Gson 构造方法只有一个! * gson.fromJson(String json,Class
) 解析 对象 * gson.fromJson(String json, Type type) 解析 数组 * Type 使用 TypeToken getType 的方式获取到 * * * FastJson * 使用JSON的静态方法 * JSON.parseJson(String json, Class); 返回值是一个Bean * JSON.parseArray(String json.Class); 返回值是一个List<> */ public class MainActivity extends AppCompatActivity { String json1 = "{'address':\"北京市\",\"id\":1001,\"name\":\"Tom\"}"; String json2 = "{\"person\":{\"address\":\"北京市\",\"id\":1001,\"name\":\"Tom\"}}"; String json3 = "{\"person1\":{\"address\":\"北京市\",\"id\":1001,\"name\":\"Tom\"},\"person2\":{\"address\":\"北京市\",\"id\":1001,\"name\":\"Tom\"}}"; String json4 = "[{\"id\":1001,\"name\":\"Tom\",\"address\":\"北京市\"},{\"id\":1002,\"name\":\"Mary\",\"address\":\"上海市\"}]"; String json5 = "{\"persons\":[{\"id\":1001,\"name\":\"Tom\",\"address\":\"北京市\"},{\"id\":1002,\"name\":\"Mary\",\"address\":\"上海市\"}]}"; String json6 = "[1,2,3,4,5,6,7,8,9,0]"; String json7 = "{\"ret\":1,\"data\":[{\"id\":\"8289\",\"title\":\"\\u6cb9\\u7116\\u5927\\u867e\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/9\\/8289.jpg\",\"collect_num\":\"1659\",\"food_str\":\"\\u5927\\u867e \\u8471 \\u751f\\u59dc \\u690d\\u7269\\u6cb9 \\u6599\\u9152\",\"num\":1659},{\"id\":\"2127\",\"title\":\"\\u56db\\u5ddd\\u56de\\u9505\\u8089\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/3\\/2127.jpg\",\"collect_num\":\"1585\",\"food_str\":\"\\u732a\\u8089 \\u9752\\u849c \\u9752\\u6912 \\u7ea2\\u6912 \\u59dc\\u7247\",\"num\":1585},{\"id\":\"30630\",\"title\":\"\\u8d85\\u7b80\\u5355\\u8292\\u679c\\u5e03\\u4e01\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/31\\/30630.jpg\",\"collect_num\":\"1528\",\"food_str\":\"QQ\\u7cd6 \\u725b\\u5976 \\u8292\\u679c\",\"num\":1528},{\"id\":\"9073\",\"title\":\"\\u5bb6\\u5e38\\u7ea2\\u70e7\\u9c7c\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/10\\/9073.jpg\",\"collect_num\":\"1419\",\"food_str\":\"\\u9c9c\\u9c7c \\u59dc \\u8471 \\u849c \\u82b1\\u6912\",\"num\":1419},{\"id\":\"10097\",\"title\":\"\\u5bb6\\u5e38\\u714e\\u8c46\\u8150\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/11\\/10097.jpg\",\"collect_num\":\"1405\",\"food_str\":\"\\u8c46\\u8150 \\u65b0\\u9c9c\\u7ea2\\u6912 \\u9752\\u6912 \\u8471\\u82b1 \\u6cb9\",\"num\":1405},{\"id\":\"10509\",\"title\":\"\\u6c34\\u716e\\u8089\\u7247\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/11\\/10509.jpg\",\"collect_num\":\"1336\",\"food_str\":\"\\u7626\\u732a\\u8089 \\u751f\\u83dc \\u8c46\\u74e3\\u9171 \\u5e72\\u8fa3\\u6912 \\u82b1\\u6912\",\"num\":1336},{\"id\":\"46968\",\"title\":\"\\u7ea2\\u7cd6\\u82f9\\u679c\\u94f6\\u8033\\u6c64\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/47\\/46968.jpg\",\"collect_num\":\"1245\",\"food_str\":\"\\u94f6\\u8033 \\u82f9\\u679c \\u7ea2\\u7cd6\",\"num\":1245},{\"id\":\"10191\",\"title\":\"\\u9ebb\\u5a46\\u8c46\\u8150\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/11\\/10191.jpg\",\"collect_num\":\"1214\",\"food_str\":\"\\u8c46\\u8150 \\u8089\\u672b \\u751f\\u62bd \\u767d\\u7cd6 \\u829d\\u9ebb\\u6cb9\",\"num\":1214},{\"id\":\"2372\",\"title\":\"\\u76ae\\u86cb\\u7626\\u8089\\u7ca5\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/3\\/2372.jpg\",\"collect_num\":\"1147\",\"food_str\":\"\\u5927\\u7c73 \\u76ae\\u86cb \\u732a\\u8089 \\u6cb9\\u6761 \\u9999\\u8471\",\"num\":1147},{\"id\":\"2166\",\"title\":\"\\u8682\\u8681\\u4e0a\\u6811\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/3\\/2166.jpg\",\"collect_num\":\"1140\",\"food_str\":\"\\u7ea2\\u85af\\u7c89 \\u8089 \\u59dc \\u849c \\u82b1\\u6912\",\"num\":1140},{\"id\":\"2262\",\"title\":\"\\u7cd6\\u918b\\u8089\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/3\\/2262.jpg\",\"collect_num\":\"1074\",\"food_str\":\"\\u732a\\u8089 \\u7ea2\\u6912 \\u9ec4\\u6912 \\u6d0b\\u8471 \\u86cb\\u6e05\",\"num\":1074},{\"id\":\"9971\",\"title\":\"\\u9c7c\\u9999\\u8c46\\u8150\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/10\\/9971.jpg\",\"collect_num\":\"1004\",\"food_str\":\"\\u8c46\\u8150 \\u6728\\u8033 \\u80e1\\u841d\\u535c \\u9999\\u8471 \\u756a\\u8304\\u9171\",\"num\":1004},{\"id\":\"10172\",\"title\":\"\\u5e72\\u7178\\u56db\\u5b63\\u8c46\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/11\\/10172.jpg\",\"collect_num\":\"988\",\"food_str\":\"\\u56db\\u5b63\\u8c46 \\u5e72\\u8fa3\\u6912 \\u849c\\u5934 \\u9171\\u6cb9 \\u7cd6\",\"num\":988},{\"id\":\"2685\",\"title\":\"\\u80e1\\u841d\\u535c\\u8089\\u672b\\u84b8\\u86cb\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/3\\/2685.jpg\",\"collect_num\":\"917\",\"food_str\":\"\\u80e1\\u841d\\u535c \\u8089 \\u86cb \\u751f\\u62bd \\u76d0\",\"num\":917},{\"id\":\"9972\",\"title\":\"\\u864e\\u76ae\\u9752\\u6912\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/10\\/9972.jpg\",\"collect_num\":\"889\",\"food_str\":\"\\u9752\\u8fa3\\u6912 \\u5927\\u849c \\u9999\\u918b \\u767d\\u7cd6 \\u751f\\u62bd\",\"num\":889},{\"id\":\"10437\",\"title\":\"\\u53c9\\u70e7\\u6392\\u9aa8\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/11\\/10437.jpg\",\"collect_num\":\"797\",\"food_str\":\"\\u6392\\u9aa8 \\u674e\\u9526\\u8bb0\\u53c9\\u70e7\\u9171 \\u690d\\u7269\\u6cb9 \\u6e05\\u6c34 \\u6cb9\\u83dc\",\"num\":797},{\"id\":\"2892\",\"title\":\"\\u201c\\u4e94\\u884c\\u201d\\u5f69\\u852c\\u6c64\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/3\\/2892.jpg\",\"collect_num\":\"756\",\"food_str\":\"\\u9ed1\\u6728\\u8033 \\u7389\\u7c73 \\u725b\\u84a1 \\u80e1\\u841d\\u535c \\u897f\\u5170\\u82b1\",\"num\":756},{\"id\":\"33783\",\"title\":\"\\u7f8e\\u4eba\\u8c46\\u6d46\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/34\\/33783.jpg\",\"collect_num\":\"753\",\"food_str\":\"\\u9ec4\\u8c46 \\u7ea2\\u8c46 \\u7eff\\u8c46 \\u9ed1\\u8c46 \\u9ed1\\u7c73\",\"num\":753},{\"id\":\"2348\",\"title\":\"\\u9ebb\\u8fa3\\u8089\\u4e1d\\u9762\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/3\\/2348.jpg\",\"collect_num\":\"753\",\"food_str\":\"\\u9762\\u6761 \\u8089\\u4e1d \\u6dc0\\u7c89 \\u9171\\u6cb9 \\u8fa3\\u6912\",\"num\":753},{\"id\":\"10044\",\"title\":\"\\u571f\\u8c46\\u7096\\u7fc5\\u6839\",\"pic\":\"http:\\/\\/www.qubaobei.com\\/ios\\/cf\\/uploadfile\\/132\\/11\\/10044.jpg\",\"collect_num\":\"752\",\"food_str\":\"\\u571f\\u8c46 \\u7fc5\\u6839 \\u8471 \\u59dc \\u6599\\u9152\",\"num\":752}]}"; private TextView showTv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showTv = (TextView) findViewById(R.id.showTv); } public void onClick(View view) { switch (view.getId()) { case R.id.bt1: //原生解析 1 try { //将 Json1 字符串转换成为一个JsonObject 对象 JSONObject jsonObject = new JSONObject(json1); //根据键 获取 值 String address = jsonObject.getString("address"); int id = jsonObject.getInt("id"); String name = jsonObject.getString("name"); showTv.setText("2: " + address + "__" + id + "__" + name); } catch (JSONException e) { e.printStackTrace(); } break; case R.id.bt2: //原生解析 2 try { //将Json2 字符串转换成为JSONObject 对象 JSONObject object = new JSONObject(json2); // 获取JSONObject 对象里面的Object 对象 JSONObject person = object.getJSONObject("person"); //根据键 获取 值 String address = person.getString("address"); int id = person.getInt("id"); String name = person.getString("name"); //展示出来 showTv.setText("2: " + address + "__" + id + "__" + name); } catch (JSONException e) { e.printStackTrace(); } break; case R.id.bt3: //原生解析 3 try { //将 Json3字符串转换成为JSONObject 对象 JSONObject object = new JSONObject(json3); //获取两个子对象 JSONObject person1 = object.getJSONObject("person1"); JSONObject person2 = object.getJSONObject("person2"); //根据键 获取 值 String address1 = person1.getString("address"); int id1 = person1.getInt("id"); String name1 = person1.getString("name"); //根据键 获取 值 String address2 = person2.getString("address"); int id2 = person2.getInt("id"); String name2 = person2.getString("name"); //展示出来 showTv.setText("3: " + address1 + "__" + id1 + "__" + name1 + "\n" + address2 + "__" + id2 + "__" + name2); } catch (JSONException e) { e.printStackTrace(); } break; case R.id.bt4: //原生解析 4 解析数组 try { //将字符串转换成为JSONArray 对象 JSONArray array = new JSONArray(json4); //循环遍历数组 for (int i = 0; i < array.length(); i++) { //获取数组中的 第i个元素 JSONObject obj = array.getJSONObject(i); //根据键 获取 值 String address = obj.getString("address"); int id = obj.getInt("id"); String name = obj.getString("name"); showTv.append("4: " + address + "__" + id + "__" + name + "\n"); } } catch (JSONException e) { e.printStackTrace(); } break; case R.id.bt5: //原生解析 5 try { JSONObject object = new JSONObject(json5); JSONArray persons = object.getJSONArray("persons"); //循环遍历 for (int i = 0; i < persons.length(); i++) { //获取第i个数据 JSONObject obj = persons.getJSONObject(i); //根据键 获取 值 String address = obj.getString("address"); int id = obj.getInt("id"); String name = obj.getString("name"); //显示出来 showTv.append("5: " + address + "__" + id + "__" + name + "\n"); } } catch (JSONException e) { e.printStackTrace(); } break; case R.id.bt6: //原生解析 6 try { JSONArray array = new JSONArray(json6); for (int i = 0; i < array.length(); i++) { int anInt = array.getInt(i); showTv.append(anInt + "_"); } } catch (JSONException e) { e.printStackTrace(); } break; case R.id.bt7: //原生解析 7 FoodEntity entity = new FoodEntity(); List list = new ArrayList<>(); try { JSONObject object = new JSONObject(json7); int ret = object.getInt("ret"); //存入对象数据 entity.setRet(ret); //获取数组 JSONArray data = object.getJSONArray("data"); //循环遍历 for (int i = 0; i < data.length(); i++) { //获取循环中的JSONObject JSONObject obj = data.getJSONObject(i); String id = obj.getString("id"); String title = obj.getString("title"); String pic = obj.getString("pic"); String collect_num = obj.getString("collect_num"); String food_str = obj.getString("food_str"); int num = obj.getInt("num"); //实例化 数组中的对象 FoodEntity.DataBean dataBean = new FoodEntity.DataBean(id,title,pic,collect_num,food_str,num); list.add(dataBean); } entity.setData(list); showTv.setText(entity.toString()); } catch (JSONException e) { e.printStackTrace(); } break; case R.id.bt8: //使用Gson解析 gson 的构造方法 只有1个 就是这个无参构造! Gson gson = new Gson(); /*解析 Json 对象的时候 *参数1 表示的是 要解析的 Json 格式的 字符串 *参数2 表示的是解析完成后的实体类 类型 */ FoodBean foodBean = gson.fromJson(json7, FoodBean.class); // showTv.setText(foodBean.toString()); //将对象转换成为json 格式的 字符串! 表示 生成 Json格式的字符串过程 String json = gson.toJson(foodBean); /* *解析Json数组的时候 */ //先获取 一个Type 类型 数据类型 泛型 是 List< Entity> Type type = new TypeToken >(){}.getType(); //在进行解析 List
people = (List ) gson.fromJson(json4, type); showTv.setText(people.toString()); //记住 create的返回值是 Gson 对象 Gson gson1 = new GsonBuilder().create(); break; case R.id.bt9: //FastJson解析 //解析对象 FoodBean bean = JSON.parseObject(json7, FoodBean.class); //解析数组 List persons = JSON.parseArray(json4, Person.class); showTv.setText(bean.toString()+persons.toString()); break; } } }
二、案例二:
Gradle文件导入依赖
compile 'com.google.code.gson:gson:2.8.2'
布局文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.mrzhao.exam1demo.ActivityA"> android:layout_width="match_parent" android:id="@+id/bt1" android:onClick="onClick" android:text="按钮1" android:layout_height="wrap_content" tools:ignore="OnClick" /> android:layout_width="match_parent" android:id="@+id/bt2" android:text="按钮2" android:onClick="onClick" android:layout_height="wrap_content" tools:ignore="OnClick" /> android:layout_width="wrap_content" android:textSize="20sp" android:id="@+id/show_tv" android:layout_margin="10dp" android:layout_height="wrap_content" />
Activity:
public class ActivityA extends AppCompatActivity { private TextView showTv; private BaWeiEntity baWeiEntity; private String json = "{\n" + " \"clazz\": [\n" + " {\n" + " \"code\": \"0100\",\n" + " \"count\": 20\n" + " },\n" + " {\n" + " \"code\": \"0101\",\n" + " \"count\": 21\n" + " },\n" + " {\n" + " \"code\": \"0102\",\n" + " \"count\": 22\n" + " },\n" + " {\n" + " \"code\": \"0103\",\n" + " \"count\": 23\n" + " }\n" + " ],\n" + " \"school\": \"八维\"\n" + "}"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showTv = (TextView) findViewById(R.id.show_tv); //设置展示的默认值 showTv.setText(json); } public void onClick(View view) { switch (view.getId()){ case R.id.bt1: //解析 Gson gson = new Gson(); baWeiEntity = gson.fromJson(json, BaWeiEntity.class); break; case R.id.bt2: //显示出来 showTv.setText(baWeiEntity.toString()); break; } } }
三:案例三:
Gradle倒入依赖
compile 'com.google.code.gson:gson:2.8.2'
布局文件:
xml version="1.0" encoding="utf-8"?>xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.mrzhao.exam2demo.MainActivity"> android:layout_width="match_parent" android:text="点击解析" android:onClick="onClick" android:layout_height="wrap_content" tools:ignore="OnClick" />
Activity:
public class MainActivity extends AppCompatActivity { private Listlist = new ArrayList<>(); private String json = "{\"status\":\"ok\",\"paramz\":{\"feeds\":[{\"id\":299063,\"oid\":288331,\"category\":\"article\",\"data\":{\"subject\":\"上海男子称劝架被戳瞎眼寻证人\",\"summary\":\"近日,上海地铁世纪大道站出站口贴出一张“寻找目击证人”的告示。\",\"cover\":\"/Attachs/Article/288331/2a6dd034cca6434b9eae169c96d5c267_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 19:42:25\"}},{\"id\":299038,\"oid\":288315,\"category\":\"article\",\"data\":{\"subject\":\"福建一干部驾车连撞3人后逃逸\",\"summary\":\"华安县一名党政干部在驾车连撞3人后逃逸,事发6小时后才向警方投案自首。\",\"cover\":\"/Attachs/Article/288315/b6e86bb108c84332821263bc93369abf_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 18:03:06\"}},{\"id\":299036,\"oid\":288313,\"category\":\"article\",\"data\":{\"subject\":\"中学防空演练致学生住院\",\"summary\":\"截至目前,仍有学生住院治疗,部分吸入烟尘较少的、反应较轻的学生已经出院。\",\"cover\":\"/Attachs/Article/288313/59232ee8363d440592d71bb61aa31cef_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 19:09:51\"}},{\"id\":299035,\"oid\":288312,\"category\":\"article\",\"data\":{\"subject\":\"惯偷被抓后自称刘谦高徒\",\"summary\":\"惯偷蒙某以草帽当掩护,从轮渡“转战”到南普陀,专找游客下手。\",\"cover\":\"/Attachs/Article/288312/28ce42849c0c47d48ae646912e2b20bb_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 17:51:14\"}},{\"id\":299033,\"oid\":288311,\"category\":\"article\",\"data\":{\"subject\":\"男子偷拍被罚赔款 报警求退款\",\"summary\":\"经过近2个小时的调解和教育,该男子再次向女大学生道歉。\",\"cover\":\"/Attachs/Article/288311/f756bcd84b674ce1bb10a9e10145a680_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 17:47:18\"}},{\"id\":299027,\"oid\":288309,\"category\":\"article\",\"data\":{\"subject\":\"台北老夫妇花60万买墓地不能用\",\"summary\":\"去年老先生因病过世,在下葬前,突然发现这块墓地属于农牧用地,没办法下葬。\",\"cover\":\"/Attachs/Article/288309/cbcb37310f0c450f964f41aac1279935_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 17:37:41\"}},{\"id\":299008,\"oid\":288298,\"category\":\"article\",\"data\":{\"subject\":\"宁波集体婚礼地铁当“婚车”\",\"summary\":\"19日,宁波轨道交通2号线一期即将试运营之际,一场特别的公益集体婚礼举行\",\"cover\":\"/Attachs/Article/288298/e108df21cb75452890f1a608b1c5181d_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 17:13:25\"}},{\"id\":299005,\"oid\":288294,\"category\":\"article\",\"data\":{\"subject\":\"4女子离家房子被倒卖蜗居楼道\",\"summary\":\"北京市公安局朝阳分局经侦大队已受理崔文花的报案,目前该案正在调查中。\",\"cover\":\"/Attachs/Article/288294/cee8182e731a4240816e62c162a30a91_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 17:48:19\"}},{\"id\":299003,\"oid\":288292,\"category\":\"article\",\"data\":{\"subject\":\"纽约女子产极低概率同卵三胞胎\",\"summary\":\"据称,自然受孕并产下同卵三胞胎的概率小到百万分之一。\",\"cover\":\"/Attachs/Article/288292/c3456fe5f50a4f44910c3b41479d909e_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 17:00:43\"}},{\"id\":298999,\"oid\":288288,\"category\":\"article\",\"data\":{\"subject\":\"大妈火车上脱鞋引发互殴\",\"summary\":\"一位中年大姐脱鞋产生异味,引起对面的年轻女子不满,双方竟然大打出手。\",\"cover\":\"/Attachs/Article/288288/61ce514a12ab4782936bea0a4abecee1_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 16:55:41\"}},{\"id\":298998,\"oid\":288287,\"category\":\"article\",\"data\":{\"subject\":\"上海男子街头持刀砍伤4名路人\",\"summary\":\"据网友爆料称,浦东张江一男子持刀伤人,4人被砍伤送医救治。\",\"cover\":\"/Attachs/Article/288287/6e74c69a400d4055b6f9070bf229b5f5_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 16:50:33\"}},{\"id\":298997,\"oid\":288286,\"category\":\"article\",\"data\":{\"subject\":\"男子盗窃后\\\"监视\\\"警察破案被抓\",\"summary\":\"接到客栈报案后,邛崃刑侦大队刑警迅速赶到现场。\",\"cover\":\"/Attachs/Article/288286/b62950e9aff64bc7972fbbcf25ca1ce4_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 16:46:59\"}},{\"id\":298996,\"oid\":288285,\"category\":\"article\",\"data\":{\"subject\":\"南京一男子闹市仿杰克逊热舞\",\"summary\":\"据围观市民反映,该男子曾在其他地段有过同样举动,疑似患有精神疾病。\",\"cover\":\"/Attachs/Article/288285/760f5a4e8a3a4cb997be165361f40fbf_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 16:41:41\"}},{\"id\":298989,\"oid\":288274,\"category\":\"article\",\"data\":{\"subject\":\"城管协管员喷水驱赶乞讨老人\",\"summary\":\"相关部门就此解释,城管协管员是不得以才出此下策。\",\"cover\":\"/Attachs/Article/288274/77e034fbd53148c8b0024dce2fd74a3f_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 16:03:12\"}},{\"id\":298987,\"oid\":288273,\"category\":\"article\",\"data\":{\"subject\":\"哲学硕士网恋被假女友骗70万\",\"summary\":\"其实“她”只是个小学文化的男人,一人饰四角,把重点大学硕士骗得团团转。\",\"cover\":\"/Attachs/Article/288273/776da3e2b8084fb7804b7da1b48e8703_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 15:57:30\"}},{\"id\":298963,\"oid\":288260,\"category\":\"article\",\"data\":{\"subject\":\"大姐脱鞋熏到女子 火车上互殴\",\"summary\":\"一位中年大姐脱鞋产生异味,引起对面的年轻女子不满,双方大打出手。\",\"cover\":\"/Attachs/Article/288260/857f0347a68e4a24bc9d05564f715584_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 15:15:34\"}},{\"id\":298962,\"oid\":288258,\"category\":\"article\",\"data\":{\"subject\":\"四川一公交太破雨天需打伞开车\",\"summary\":\"市民爆料称,南充901路公交司机打着伞开车,真佩服司机的敬业精神。\",\"cover\":\"/Attachs/Article/288258/81ddda6bccd9424b800d4320dc23ff7e_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 15:13:04\"}},{\"id\":298950,\"oid\":288249,\"category\":\"article\",\"data\":{\"subject\":\"2女生被撞死 母亲路中抱紧尸体\",\"summary\":\"福建泉州,两名初中女生上学路上被撞身亡,司机逃逸被逼停。\",\"cover\":\"/Attachs/Article/288249/a169f8f3488c43fea756228f3f9c34af_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 14:55:06\"}},{\"id\":298949,\"oid\":288248,\"category\":\"article\",\"data\":{\"subject\":\"宁波轨交集体婚礼 地铁当婚车\",\"summary\":\"地铁车厢当婚车,梁祝公园扮婚房,一场特别的公益集体婚礼在宁波举行。\",\"cover\":\"/Attachs/Article/288248/250078f30b9a43e399e3f7c53012034b_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 14:52:42\"}},{\"id\":298943,\"oid\":288247,\"category\":\"article\",\"data\":{\"subject\":\"南京苹果直营店果粉彻夜排队\",\"summary\":\"9月19日上午,苹果南京首家直营店即将开业,果粉彻夜排队守候。\",\"cover\":\"/Attachs/Article/288247/dd4ee3bbaeb343f59c1bd125808f530d_padmini.JPG\",\"pic\":\"\",\"format\":\"txt\",\"changed\":\"2015-09-19 14:48:44\"}}],\"PageIndex\":1,\"PageSize\":20,\"TotalCount\":27860,\"TotalPage\":1393}}"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void onClick(View view) { //点击进行解析 Gson gson = new Gson(); NewsEntity newsEntity = gson.fromJson(json, NewsEntity.class); //先找到数据中的集合 List feeds = newsEntity.getParamz().getFeeds(); //循环遍历 for (int i = 0; i < feeds.size(); i++) { //获取每个对象中的 Subject String subject = feeds.get(i).getData().getSubject(); //放到集合中 list.add(subject); } //遍历Subject集合 for (int i = 0; i < list.size(); i++) { //将集合中的数据写入文件里 try { //暂时没有学习存储 没有文件路径 FileOutputStream outputStream = new FileOutputStream(new File("path")); String s = list.get(i); byte[] bytes = s.getBytes(); outputStream.write(bytes, 0, bytes.length); outputStream.flush(); outputStream.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } }