今天分享的是Json数据手动解析,在开发过程中,难免会遇到手动解析的情况,但是很多时候不知从何入手,周围的同事们也经常问怎样手动解析,今天通过一个例子为大家演示一遍。
演示用的Json数据接口:
http://ajax.googleapis.com/ajax/services/feed/load?q=http://www.bilibili.tv/rss-13.xml&v=1.0
Json数据:
{
"responseData": {
"feed": {
"author": "",
"description": "番剧",
"entries": [
{
"author": "哔哩哔哩番剧",
"categories": [
"连载动画"
],
"content": "#03 豚骨杯面与螺旋吸吸冻与…",
"contentSnippet": "#03 豚骨杯面与螺旋吸吸冻与…",
"link": "http://www.bilibili.com/video/av3635899/",
"publishedDate": "Fri, 22 Jan 2016 05:30:07 -0800",
"title": "【1月】粗点心战争 03【独家正版】"
},
{
"author": "夯大力007",
"categories": [
"国产动画"
],
"content": "自制 我没醉",
"contentSnippet": "自制 我没醉",
"link": "http://www.bilibili.com/video/av3635775/",
"publishedDate": "Fri, 22 Jan 2016 05:03:45 -0800",
"title": "大力金刚 148 醉生梦死"
},
{
"author": "方块动漫",
"categories": [
"国产动画"
],
"content": "自制 「维和队」经过一轮追踪,发现大黑影沉降海底后便消失。于是用钱博士发明的新装备「水中推进器」在水底进行调查,发现了大黑影的真面目是……",
"contentSnippet": "自制 「维和队」经过一轮追踪,发现大黑影沉降海底后便消失。于是用钱博士发明的新装备「水中推进器」在水底进行调查,发现了大黑影的真面目是……",
"link": "http://www.bilibili.com/video/av3635410/",
"publishedDate": "Fri, 22 Jan 2016 04:05:22 -0800",
"title": "正义红师 粤语版 09"
},
{
"author": "哔哩哔哩番剧",
"categories": [
"连载动画"
],
"content": "#02 桃源乡的结局",
"contentSnippet": "#02 桃源乡的结局",
"link": "http://www.bilibili.com/video/av3635276/",
"publishedDate": "Fri, 22 Jan 2016 03:47:12 -0800",
"title": "【1月】从前有座灵剑山 日文版 02"
}
],
"feedUrl": "http://www.bilibili.tv/rss-13.xml",
"link": "http://www.bilibili.com/video/bangumi.html",
"title": "番剧",
"type": "rss20"
}
},
"responseStatus": 200
}
完整的类如下:
public class JsonObj {
public ResponseDataEntity responseData;
public int responseStatus;
//自己解析json字符并返回解析好的本类的对象
public static JsonObj parse(String str) {
JsonObj jsonObj=null;
if(!TextUtils.isEmpty(str)){
try {
//初始化JsonObj对象,此处可不是JSONObject,只是名字像而已
jsonObj=new JsonObj();
//根据json字符串生成JSONObject
JSONObject object=new JSONObject(str);
//ResponseDataEntity根据得到的对应的字符串:object.optJSONObject("feed").toString(),解析并返回自己的对象
jsonObj.responseData=ResponseDataEntity.parse(object.optJSONObject("responseData").toString());
jsonObj.responseStatus=object.optInt("responseStatus");
} catch (Exception e) {
e.printStackTrace();
}
}
return jsonObj;
}
public static class ResponseDataEntity {
public FeedEntity feed;
//自己解析json字符并返回解析好的本类的对象
public static ResponseDataEntity parse(String str) {
ResponseDataEntity responseDataEntity=null;
if(!TextUtils.isEmpty(str)){
try {
responseDataEntity=new ResponseDataEntity();
//根据json字符串生成JSONObject
JSONObject object=new JSONObject(str);
//FeedEntity根据得到的对应的字符串:object.optJSONObject("feed").toString(),解析并返回自己的对象
responseDataEntity.feed=FeedEntity.parse(object.optJSONObject("feed").toString());
} catch (Exception e) {
e.printStackTrace();
}
}
return responseDataEntity;
}
public static class FeedEntity {
public String author;
public String description;
public String feedUrl;
public String link;
public String title;
public String type;
public List<EntriesEntity> entries;
//自己解析json字符并返回解析好的本类的对象
public static FeedEntity parse(String str) {
FeedEntity feedEntity=null;
if(!TextUtils.isEmpty(str)){
feedEntity=new FeedEntity();
try {
//解析各个字段
JSONObject feedobj=new JSONObject(str);
feedEntity.author=feedobj.optString("author");
feedEntity.description=feedobj.optString("description");
feedEntity.feedUrl=feedobj.optString("feedUrl");
feedEntity.link=feedobj.optString("link");
feedEntity.title=feedobj.optString("title");
feedEntity.type=feedobj.optString("type");
//解析集合对象,此处集合是EntriesEntity对象的集合
JSONArray array=feedobj.optJSONArray("entries");
if(array!=null){
List<EntriesEntity> list=new ArrayList<EntriesEntity>();
for (int i = 0; i <array.length() ; i++) {
//EntriesEntity根据解析出来的对应字符串array.optJSONObject(i).toString():,解析并返回自己的对象
EntriesEntity entity = EntriesEntity.parse(array.optJSONObject(i).toString());
list.add(entity);//加入到集合
}
feedEntity.entries=list;//赋值集合字段
}
} catch (Exception e) {
e.printStackTrace();
}
}
return feedEntity;
}
public static class EntriesEntity {
public String author;
public String content;
public String contentSnippet;
public String link;
public String publishedDate;
public String title;
public List<String> categories;
//自己解析json字符并返回解析好的本类的对象
public static EntriesEntity parse(String str) {
EntriesEntity entriesEntity=null;
if(!TextUtils.isEmpty(str)){//不为空
try {
//解析各个字段
entriesEntity=new EntriesEntity();
JSONObject jsonObject=new JSONObject(str);
entriesEntity.author=jsonObject.optString("author");
entriesEntity.content=jsonObject.optString("content");
entriesEntity.contentSnippet=jsonObject.optString("contentSnippet");
entriesEntity.link=jsonObject.optString("link");
entriesEntity.publishedDate=jsonObject.optString("publishedDate");
entriesEntity.title=jsonObject.optString("title");
//解析集合对象,此处的集合是字符串集合
JSONArray jsonArray=jsonObject.optJSONArray("categories");
if(jsonArray!=null){
List<String> list=new ArrayList<String>();
for (int i = 0; i < jsonArray.length(); i++) {
list.add(jsonArray.optString(i));//把解析出来的字符串加入集合
}
entriesEntity.categories=list;
}
} catch (Exception e) {
e.printStackTrace();
}
}
return entriesEntity;
}
}
}
}
}
使用方法
JsonObj myobj=JsonObj.parse(jsonstr);//jsonstr 就是上面的json数据
这种解析方式是由里到外,注意方法调用,一直调用到最里面那个类的parse方法,然后再依次返回