作为一个新手。。每次解析数据真心累,我之前解析JSON数据是这样解析的
JSONObject jsonObjs = new JSONObject(fanhui);
JSONArray jsonObja = jsonObjs.getJSONArray("data");
if (jsonObja != null) {
newlist = new ArrayList>();
for (int i = 0; i < jsonObja.length(); i++) {
JSONObject jsonObj = jsonObja.getJSONObject(i);
HashMap map = new HashMap();
map.put("dc", jsonObj.getString("dc"));
map.put("type", jsonObj.getString("type"));
map.put("sn", jsonObj.getString("sn"));
map.put("time", jsonObj.getString("time"));
map.put("model", jsonObj.getString("model"));
map.put("station", jsonObj.getString("station"));
map.put("repair_engneer", jsonObj.getString("repair_engneer"));
map.put("partsn", jsonObj.getString("partsn"));
map.put("location", jsonObj.getString("location"));
map.put("description", jsonObj.getString("description"));
map.put("failReason", jsonObj.getString("failReason"));
map.put("lc", jsonObj.getString("lc"));
map.put("dc", jsonObj.getString("dc"));
map.put("manufacturer", jsonObj.getString("manufacturer"));
newlist.add(map);
然后每次解析对我来说都是CNM。内心崩溃的。后来看书和看别人的文章知道了GSON。。就各种百度使用方法。。接下来我就教大家使用方法。
1.解析。普通型数据就是没有数据头的。
获取到JSON数据之后,先转化为String,我这里用的是OKHTTP
final String res = response.body().string();
然后用Handler或者新开线程去解析跟新UI
runOnUiThread(new Runnable() { @Override public void run() {
Gson gson = new Gson(); login lo = gson.fromJson(res,login.class); //这里login是一个实体类。就是把你要解析的数据自己新建一个实体类。 cookie.setText(lo.getChinesename());//这个cookie是一个TextView,这一步就是显示其中的chinesename到控件上。} });
就是这样新建实体类。
public class login { private int code; public String getChinesename() { return chinesename; } public void setChinesename(String chinesename) { this.chinesename = chinesename; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } private String chinesename; }
第二种方式。不管解析多么复杂的JSON数据。多少个数据头。都不用担心。
{ "reason": "成功的返回", "result": { "stat": "1", "data": [ { "uniquekey": "f907b629f6c912fc07431de58d741481", "title": "蒋劲夫最新广告大片曝光 篮球boy目光如炬", "date": "2017-02-28 08:34", "category": "头条", "author_name": "新华网", "url": "http://mini.eastday.com/mobile/170228083404688.html", "thumbnail_pic_s": "http://09.imgmini.eastday.com/mobile/20170228/20170228083404_4bb9f28c06311beb3617e92caf8d6d0d_1_mwpm_03200403.jpeg", "thumbnail_pic_s02": "http://09.imgmini.eastday.com/mobile/20170228/20170228083404_69bacbdcc57d97f7bfe76610a5f6ad66_2_mwpm_03200403.jpeg", "thumbnail_pic_s03": "http://09.imgmini.eastday.com/mobile/20170228/20170228083404_34739a168481ee9c81541bc0bb2a3791_3_mwpm_03200403.jpeg" }, { "uniquekey": "2446368ff46178d5a96118c634e90fd5", "title": "澳大利亚华裔被灭门凶手收养 庭上指证曾遭其多次性侵", "date": "2017-02-28 08:47", "category": "头条", "author_name": "环球网", "url": "http://mini.eastday.com/mobile/170228084744624.html", "thumbnail_pic_s": "http://09.imgmini.eastday.com/mobile/20170228/20170228084744_f52b522e254792801c171d9fed92258a_1_mwpm_03200403.jpeg" }, { "uniquekey": "88c225a97b514604494873fd4894dbb8", "title": "央媒刊文谈丽江古城被严重警告:整治旅游市场乱象必痛下狠手", "date": "2017-02-28 08:47", "category": "头条", "author_name": "中国青年报", "url": "http://mini.eastday.com/mobile/170228084707053.html", "thumbnail_pic_s": "http://04.imgmini.eastday.com/mobile/20170228/20170228084707_6feaabd5a1b29cd86dfef8b1853dc144_1_mwpm_03200403.jpeg" }, { "uniquekey": "b40e26ff6346bfc57debea383e99497b", "title": "连续五年“当头炮”简政放权让更多权力公章不再任性", "date": "2017-02-28 08:41", "category": "头条", "author_name": "中国青年网", "url": "http://mini.eastday.com/mobile/170228084111459.html", "thumbnail_pic_s": "http://09.imgmini.eastday.com/mobile/20170228/20170228084111_59f901484a2bab5eb3c1bac8dc0f6de5_1_mwpm_03200403.jpeg" }, { "uniquekey": "7d1caf9baaa9b9bf0152321f2f271fb9", "title": "《花儿与少年3》录制 湖南台为什么选择这8位明星?", "date": "2017-02-28 08:31", "category": "头条", "author_name": "TV哔", "url": "http://mini.eastday.com/mobile/170228083147895.html", "thumbnail_pic_s": "http://02.imgmini.eastday.com/mobile/20170228/20170228083147_32a261122ad6912ba4307b18759c02ba_1_mwpm_03200403.jpeg", "thumbnail_pic_s02": "http://02.imgmini.eastday.com/mobile/20170228/20170228083147_32a261122ad6912ba4307b18759c02ba_2_mwpm_03200403.jpeg", "thumbnail_pic_s03": "http://02.imgmini.eastday.com/mobile/20170228/20170228083147_32a261122ad6912ba4307b18759c02ba_3_mwpm_03200403.jpeg" }, { "uniquekey": "8c159593caf7b99ea7800f006c1431cd", "title": "89届奥斯卡闹出史上最大乌龙 这个锅到底谁背?", "date": "2017-02-28 08:22", "category": "头条", "author_name": "搜狐娱乐", "url": "http://mini.eastday.com/mobile/170228082256576.html", "thumbnail_pic_s": "http://06.imgmini.eastday.com/mobile/20170228/20170228082256_5a77dc1e74820fe4bb8113e0b1aa4b8b_1_mwpm_03200403.jpeg", "thumbnail_pic_s02": "http://06.imgmini.eastday.com/mobile/20170228/20170228082256_5a77dc1e74820fe4bb8113e0b1aa4b8b_2_mwpm_03200403.jpeg" } ] }, "error_code": 0 }
假设上面就是要解析的JSON数据。
首先你先新建实体类。Android Studio有一个GSONFORMAT 的功能。就是你把这个JSON数据复制进去,会自动帮你生成实体类。
你先新建一个CLASS 假设为News 然后在新建的Class里面点右键选择Generate.在选择GsonForMat 把JSON数据复制进去。就可以了。
就会自动帮你生成实体类。
实体类生成之后。在回到Activity中。
把要解析的JSON数据转为String.
然后开始解析。
比如我这里想要解析Data里面的数据
Gson gson = new Gson(); News news= gson.fromJson(arg0,News.class); ArrayListdataBeen = (ArrayList ) news.getResult().getData();
然后你要显示文字的话
GSON.ResultBean.DataBean b = list.get(position); tv.setText(b.getTitle());
显示图片的话//c 是context
Picasso.with(c).load(list.get(position).getThumbnail_pic_s()).placeholder(R.mipmap.morentu).into(holder.picture);