解析新浪微博JSON数据
这里讲的是通过retrofit2请求方式得到的返回值
一、自己分析解析
默认通过responseBody
将得到的返回值
try {
str=response.body().string(); // 要用string() 不是tostring 这样才是JSONSstring格式,否则传递不了给JSONString
} catch (IOException e) {
e.printStackTrace();
}
返回的JSON层次结构可以参考官方API:
http://open.weibo.com/wiki/2/statuses/home_timeline
开始解析
根据项目需要,这里只解析了: (需要其他的根据不同层次按下面的来就好了)
微博主的图片 profile_image_url
微博主名 user里的 screen_name
发布时间 created_at
来源 client_mblogid
文字内容 text
图片内容(缩略图) pic_urls数组里的(thumbnail_pic)
图片内容(中图) bmiddle_pic(但是好像只给一张)
图片内容(原图) original_pic (但是好像只给一张)
转发数 reposts_count
评论数 comments_count
点赞数 attitudes_count
转发字段: retweeted_status
转发字段中的内容: text
转发字段中的图片: pic_urls
其实转发字段和发布的信息是一样的参数,只是转发字段是在发布的信息中的一个字字段
这里的图片内容用二维数组
因为方便去适配RecyclerView
//开始解析
try {
// 将返回的JSONString格式赋予JSONObject
JSONObject jsonObject = new JSONObject(str);
// 第一层为statuses 并且statuses为数组形式 因此将其设置为JSONArray形式
JSONArray statusesArr = jsonObject.getJSONArray("statuses");
// 循环地去获得多条微博
// 这里的index+1代表的是第n条status(微博),因此index=0 为第一条 以此类推
for (int i = 0; i < COUNT; i++) {
// 第一条 下标从0开始
JSONObject statusesObj = statusesArr.getJSONObject(i);
// statuses中的第一层,即总的第二层
// 该条微博的创建时间
created_at[i] = statusesObj.getString("created_at");
Log.i("created_at", created_at[i]);
//该条微博的来源
mSource[i] = statusesObj.getString("source");
int firstIndex=mSource[i].indexOf(">");
int lastIndex=mSource[i].lastIndexOf("<");
if(firstIndex!=-1&&lastIndex!=-1) { // 新浪微博这里的来源可能为空 因此要加这个判断
source[i] = mSource[i].substring(firstIndex + 1, lastIndex);
Log.i("source", source[i]);
}else{
source[i] = "";
}
//该条微博的转发数
reposts_count[i] = statusesObj.getString("reposts_count");
Log.i("reposts_count", reposts_count[i]);
//该条微博的评论数
comments_count[i] = statusesObj.getString("comments_count");
Log.i("comments_count", comments_count[i]);
//该条微博的点赞数
attitudes_count[i] = statusesObj.getString("attitudes_count");
Log.i("attitudes_count", attitudes_count[i]);
//该条微博的微博text内容
text[i] = statusesObj.getString("text");
Log.i("text", text[i]);
// 总的第三层
//获取图片资源的地址 数组形式
JSONArray imageArr = statusesObj.getJSONArray("pic_urls");
// 循环地去获得多个图片地址
// 获得数组的长度
//为什么这里是0张?
Log.i("图片有多少张", String.valueOf(imageArr.length()));
for(int j=0;j<imageArr.length();j++){
JSONObject imageObject = imageArr.getJSONObject(j);
pic_urls[i][j]=imageObject.getString("thumbnail_pic");
Log.i("thumbnail_pic",pic_urls[i][j]);
}
// 获取user的值
String user = statusesObj.getString("user");
//user转化为JSONObject
JSONObject userObj = new JSONObject(user); // 将其转化为JSONObject
//微博主名
screen_name[i] = userObj.getString("screen_name"); // 使用getXX方法获取数据
Log.i("screen_name", screen_name[i]);
//微博主照片的地址
profile_image_url[i] = userObj.getString("profile_image_url"); // 使用getXX方法获取数据
Log.i("profile_image_url", profile_image_url[i]);
}
// 若成功传入最后一个微博 指定一个变量 而不是只是一个数值 因为网络请求要花时间
if(profile_image_url[COUNT-1]!=null){
//若有值
canIntent=true;
// 非Activity中跳转 满足MVVM吗 不满足要改~~~~~~
Intent intent = new Intent(mContext, MainActivity.class);
mContext.startActivity(intent);
}else{
canIntent=false;
}
} catch(JSONException e){
e.printStackTrace();
}
}
二、通过JSONFormat解析
通过call<类A<类B>>形式