首页 > 学院 > 开发设计 > 正文

json解析

2019-11-08 00:35:47
字体:
来源:转载
供稿:网友

json和xml的解析有一些类似,将json的内容读入字符串之后 在进行解析 json数据是:{ }包裹的是JSONObject,对象里面是键值对,直接调用getString()等等就可以 []包裹的是JSONArray

PRivate void parseJSONWithJSONObject(String jsonData){ try{ //JSONArray jsonArray = new JSONArray(jsonData); JSONObject jsonObject = new JSONObject(jsonData); String str1 = jsonObject.getString("stories"); String str2 = jsonObject.getString("top_stories"); JSONArray jsonArray1 = new JSONArray(str1); JSONArray jsonArray2 = new JSONArray(str2); for(int i=0;i<jsonArray1.length();i++){ JSONObject jsonObject1 = jsonArray1.getJSONObject(i); int type = jsonObject1.getInt("type"); int id = jsonObject1.getInt("id"); String title = jsonObject1.getString("title"); Why temp = new Why(type,id,title); // String list = "type : "+type+" id : "+id+" title : "+title; data.add(temp); /* Log.d("type",""+type); Log.d("id",""+id); Log.d("title",title);*/ } for(int i=0;i<jsonArray2.length();i++){ JSONObject jsonObject2 = jsonArray2.getJSONObject(i); int type = jsonObject2.getInt("type"); int id = jsonObject2.getInt("id"); String title = jsonObject2.getString("title"); Why temp = new Why(type,id,title); // String list = "type : "+type+" id : "+id+" title : "+title; data.add(temp); /* Log.d("type",""+type); Log.d("id",""+id); Log.d("title",title);*/ } }catch (Exception e){ e.printStackTrace(); } }
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表