从新浪微博服务器获取到了好友微博的json数据后如何去解析着实让我费了一番功夫。因为返回的json数据格式比较复杂。下面我就通过代码说明一下如何来解析新浪微博的json数据。先来看一下从服务器端返回的json数据:
{
"statuses": [
{
"created_at": "Thu Aug 13 18:03:49 +0800 2015",
"id": 3875439447865330,
"mid": "3875439447865330",
"idstr": "3875439447865330",
"text": "9种美味炒饭的做法。吃货们get起来!",
"source_allowclick": 0,
"source_type": 1,
"source": "<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a>",
"favorited": false,
"truncated": false,
"in_reply_to_status_id": "",
"in_reply_to_user_id": "",
"in_reply_to_screen_name": "",
"pic_urls": [
{
"thumbnail_pic": "http://ww4.sinaimg.cn/thumbnail/005G0GAnjw1euzovt1b1qj30c80cjt9p.jpg"
},
{
"thumbnail_pic": "http://ww3.sinaimg.cn/thumbnail/005G0GAnjw1euzovta9jxj30c80dmq4l.jpg"
},
{
"thumbnail_pic": "http://ww2.sinaimg.cn/thumbnail/005G0GAnjw1euzovtayy3j30c80ed0u3.jpg"
},
{
"thumbnail_pic": "http://ww3.sinaimg.cn/thumbnail/005G0GAnjw1euzovtdy2rj30c80fiq4g.jpg"
},
{
"thumbnail_pic": "http://ww2.sinaimg.cn/thumbnail/005G0GAnjw1euzovtnzt9j30c80fcwg0.jpg"
},
{
"thumbnail_pic": "http://ww3.sinaimg.cn/thumbnail/005G0GAnjw1euzovtr54tj30c80erabn.jpg"
},
{
"thumbnail_pic": "http://ww1.sinaimg.cn/thumbnail/005G0GAnjw1euzovtvg1wj30c80h9jt3.jpg"
},
{
"thumbnail_pic": "http://ww3.sinaimg.cn/thumbnail/005G0GAnjw1euzovu0wv0j30c80cgq3v.jpg"
},
{
"thumbnail_pic": "http://ww1.sinaimg.cn/thumbnail/005G0GAnjw1euzovu7r4cj30c80f875y.jpg"
}
],
"thumbnail_pic": "http://ww4.sinaimg.cn/thumbnail/005G0GAnjw1euzovt1b1qj30c80cjt9p.jpg",
"bmiddle_pic": "http://ww4.sinaimg.cn/bmiddle/005G0GAnjw1euzovt1b1qj30c80cjt9p.jpg",
"original_pic": "http://ww4.sinaimg.cn/large/005G0GAnjw1euzovt1b1qj30c80cjt9p.jpg",
"geo": null,
"user": {
"id": 3613440743,
"idstr": "3613440743",
"class": 1,
"screen_name": "吃货频道",
"name": "吃货频道",
"province": "82",
"city": "2",
"location": "澳门 花地玛堂区",
"description": "搜罗全球各地美食,吃货请关注",
"url": "",
"profile_image_url": "http://tp4.sinaimg.cn/3613440743/50/5725083848/0",
"profile_url": "u/3613440743",
"domain": "",
"weihao": "",
"gender": "f",
"followers_count": 1601744,
"friends_count": 110,
"pagefriends_count": 10,
"statuses_count": 32255,
"favourites_count": 2,
"created_at": "Sun Sep 22 00:01:10 +0800 2013",
"following": true,
"allow_all_act_msg": false,
"geo_enabled": true,
"verified": true,
"verified_type": 0,
"remark": "",
"ptype": 0,
"allow_all_comment": true,
"avatar_large": "http://tp4.sinaimg.cn/3613440743/180/5725083848/0",
"avatar_hd": "http://ww4.sinaimg.cn/crop.0.29.370.370.1024/d760bae7jw1erk48zdmxyj20ac0b5q3o.jpg",
"verified_reason": "知名美食资讯博主",
"verified_trade": "1153",
"verified_reason_url": "",
"verified_source": "",
"verified_source_url": "",
"verified_state": 0,
"verified_level": 3,
"verified_reason_modified": "",
"verified_contact_name": "",
"verified_contact_email": "",
"verified_contact_mobile": "",
"follow_me": false,
"online_status": 0,
"bi_followers_count": 0,
"lang": "zh-cn",
"star": 0,
"mbtype": 2,
"mbrank": 1,
"block_word": 0,
"block_app": 0,
"credit_score": 80,
"user_ability": 0,
"urank": 13
},
"reposts_count": 5,
"comments_count": 0,
"attitudes_count": 8,
"mlevel": 0,
"visible": {
"type": 0,
"list_id": 0
},
"darwin_tags": [],
"rid": "0_0_1_2666872050318022253"
},
{
"created_at": "Thu Aug 13 18:02:32 +0800 2015",
"id": 3875439128973390,
"mid": "3875439128973390",
"idstr": "3875439128973390",
"text": "在绿水漫堤的阳朔,无论骑行 、漫步、顺水漂流还是什么都不做,都能让你找到契合的释放点,再者,追随刘三姐歌声,或停于古镇,闻三花,尝桂香。",
"source_allowclick": 0,
......
由于json数据很长,所以我只贴出了bufe。下面上代码
package com.lql.json;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.lql.bean.Status;
import com.lql.bean.User;
public class WeiboJsonTools {
/**
* 解析微博json数据
*
*
* @param jsonString
* @return
*/
public static List<Status> parseJsonToStatus(String jsonString) {
List<Status> weibos = new ArrayList<Status>();
JSONObject jsonObject;
try {
jsonObject = new JSONObject(jsonString);
JSONArray jsonArray = jsonObject.getJSONArray("statuses");//取得statuses后的JSONObject数组
//遍历jsonObject数组
for (int i = 0; i < jsonArray.length(); i++) {
Status weibo = new Status();//创建一个微博对象
JSONObject jo = jsonArray.getJSONObject(i);
String weibo_create_at = jo.getString("created_at");// 微博创建时间
long weibo_id = jo.getLong("id");// 微博ID
String text = jo.getString("text");// 微博内容
String source = jo.getString("source");// 微博来源
int reposts_count = jo.getInt("reposts_count");// 转发数
int comments_count = jo.getInt("comments_count");// 评论数
int attitudes_count = jo.getInt("attitudes_count");
Boolean favorited = jo.getBoolean("favorited");// 是否已被收藏
boolean isHaveImage = false;// 标志该微博是否配图
List<String> pic_urls = new ArrayList<String>();//微博配图可能有多张,我将其存入集合中
JSONArray jsonArray2 = jo.getJSONArray("pic_urls");// 获取图片地址
for (int j = 0; j < jsonArray2.length(); j++) {
isHaveImage = true;
JSONObject jsonObject2 = jsonArray2.getJSONObject(j);
String thumbnail_pic = jsonObject2
.getString("thumbnail_pic");
pic_urls.add(thumbnail_pic);
}
weibo.setId(weibo_id);
weibo.setCreated_at(weibo_create_at);
weibo.setText(text);
weibo.setSource(source);
weibo.setComments_count(comments_count);
weibo.setReposts_count(reposts_count);
weibo.setAttitudes_count(attitudes_count);
weibo.setFavorited(favorited);
weibo.setHaveImage(isHaveImage);
weibo.setPic_urls(pic_urls);
JSONObject jo2 = jo.getJSONObject("user");// 获取weibojson中的UserjsonObject
User user = new User();
long id = jo2.getLong("id");// 发表该微博的用户ID
String user_name = jo2.getString("screen_name");// 用户名
String location = jo2.getString("location");// 用户所在地
String description = jo2.getString("description");// 用户个人描述
String profile_image_url = jo2.getString("profile_image_url");// 用户头像地址
String profile_url = jo2.getString("profile_url");// 用户微博统一访问地址
int statuses_count = jo2.getInt("statuses_count");// 用户微博数
String gender = jo2.getString("gender");// 性别,m:男、f:女、n:未知
int followers_count = jo2.getInt("followers_count");// 粉丝数
int friends_count = jo2.getInt("friends_count");// 好友数
int favourites_count = jo2.getInt("favourites_count");// 收藏数
String created_at = jo2.getString("created_at");// 用户创建(注册)时间
boolean geo_enabled = jo2.getBoolean("geo_enabled");// 是否允许显示地理信息
boolean allow_all_comment = jo2.getBoolean("allow_all_comment");// 是否允许所有人对我的微博进行评论,true:是,false:否
String avatar_large = jo2.getString("avatar_large");// 用户头像地址(大图),180×180像素
String avatar_hd = jo2.getString("avatar_hd");// 用户高清头像
int online_status = jo2.getInt("online_status");// 用户的在线状态,0:不在线、1:在线
user.setScreen_name(user_name);
user.setAllow_all_comment(allow_all_comment);
user.setId(id);
user.setLocation(location);
user.setDescription(description);
user.setProfile_image_url(profile_image_url);
user.setProfile_url(profile_url);
user.setStatuses_count(statuses_count);
user.setFollowers_count(followers_count);
user.setGender(gender);
user.setFriends_count(friends_count);
user.setFavourites_count(favourites_count);
user.setCreated_at(created_at);
user.setGeo_enabled(geo_enabled);
user.setAllow_all_comment(allow_all_comment);
user.setAvatar_hd(avatar_hd);
user.setAvatar_large(avatar_large);
user.setOnline_status(online_status);
weibo.setUser(user);
weibos.add(weibo);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return weibos;
}
}
代码中的注释已经说说的很详细了,我采用的是android自带的JSON解析API,不用额外导入jar包,否则会报错。
下面是微博类Status和用户类User
package com.lql.bean;
import java.util.List;
/**
* 微博类
*
* @author Administrator
*
*/
public class Status {
@Override
public String toString() {
return "Status [created_at=" + created_at + ", id=" + id + ", text="
+ text + ", source=" + source + ", favorited=" + favorited
+ ", thumbnail_pic=" + thumbnail_pic + ", user=" + user
+ ", reposts_count=" + reposts_count + ", comments_count="
+ comments_count + ", visible=" + visible + ", isHaveImage="
+ isHaveImage + ", pic_urls=" + pic_urls + "]";
}
/**
* 微博创建时间
*/
private String created_at;
/**
* 微博ID
*/
private long id;
/**
* 微博内容
*/
private String text;
/**
* 微博来源
*/
private String source;
/**
* 是否已收藏,true:是,false:否
*/
private boolean favorited;
/**
* 缩略图片地址,没有时不返回此字段
*/
private String thumbnail_pic;
/**
* 微博作者的用户信息字段
*/
private User user;
/**
* 转发数
*/
private int reposts_count;
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public boolean isFavorited() {
return favorited;
}
public void setFavorited(boolean favorited) {
this.favorited = favorited;
}
public String getThumbnail_pic() {
return thumbnail_pic;
}
public void setThumbnail_pic(String thumbnail_pic) {
this.thumbnail_pic = thumbnail_pic;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public int getReposts_count() {
return reposts_count;
}
public void setReposts_count(int reposts_count) {
this.reposts_count = reposts_count;
}
public int getComments_count() {
return comments_count;
}
public void setComments_count(int comments_count) {
this.comments_count = comments_count;
}
public Object getVisible() {
return visible;
}
public void setVisible(Object visible) {
this.visible = visible;
}
public boolean isHaveImage() {
return isHaveImage;
}
public void setHaveImage(boolean isHaveImage) {
this.isHaveImage = isHaveImage;
}
public List<String> getPic_urls() {
return pic_urls;
}
public void setPic_urls(List<String> pic_urls) {
this.pic_urls = pic_urls;
}
/**
* 评论数
*/
private int comments_count;
/**
* 点赞数
*/
private int attitudes_count;
public int getAttitudes_count() {
return attitudes_count;
}
public void setAttitudes_count(int attitudes_count) {
this.attitudes_count = attitudes_count;
}
/**
* 微博的可见性及指定可见分组信息。该object中type取值,0:普通微博,1:私密微博,3:指定分组微博,4:密友微博;
* list_id为分组的组号
*/
private Object visible;
/**
* 微博是否有配图,true有 false:没有
*/
private boolean isHaveImage;
List<String> pic_urls;
}
package com.lql.bean;
import android.graphics.Bitmap;
public class User {
@Override
public String toString() {
return "User [id=" + id + ", screen_name=" + screen_name
+ ", location=" + location + ", description=" + description
+ ", profile_image_url=" + profile_image_url + ", profile_url="
+ profile_url + ", statuses_count=" + statuses_count
+ ", gender=" + gender + ", followers_count=" + followers_count
+ ", friends_count=" + friends_count + ", favourites_count="
+ favourites_count + ", created_at=" + created_at
+ ", geo_enabled=" + geo_enabled + ", allow_all_comment="
+ allow_all_comment + ", avatar_large=" + avatar_large
+ ", avatar_hd=" + avatar_hd + ", online_status="
+ online_status + ", bi_followers_count=" + bi_followers_count
+ "]";
}
/**
* 用户UID
*/
private long id;
/**
* 用户昵称
*/
private String screen_name;
/**
* 用户所在地
*/
private String location;
/**
* 用户个人描述
*/
private String description;
/**
* 用户头像地址(中图),50×50像素
*/
private String profile_image_url;
/**
* 用户的微博统一URL地址
*/
private String profile_url;
/**
* 用户微博数
*/
private int statuses_count;
public int getStatuses_count() {
return statuses_count;
}
public void setStatuses_count(int statuses_count) {
this.statuses_count = statuses_count;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getScreen_name() {
return screen_name;
}
public void setScreen_name(String screen_name) {
this.screen_name = screen_name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getProfile_image_url() {
return profile_image_url;
}
public void setProfile_image_url(String profile_image_url) {
this.profile_image_url = profile_image_url;
}
public String getProfile_url() {
return profile_url;
}
public void setProfile_url(String profile_url) {
this.profile_url = profile_url;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getFollowers_count() {
return followers_count;
}
public void setFollowers_count(int followers_count) {
this.followers_count = followers_count;
}
public int getFriends_count() {
return friends_count;
}
public void setFriends_count(int friends_count) {
this.friends_count = friends_count;
}
public int getFavourites_count() {
return favourites_count;
}
public void setFavourites_count(int favourites_count) {
this.favourites_count = favourites_count;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public boolean isGeo_enabled() {
return geo_enabled;
}
public void setGeo_enabled(boolean geo_enabled) {
this.geo_enabled = geo_enabled;
}
public boolean isAllow_all_comment() {
return allow_all_comment;
}
public void setAllow_all_comment(boolean allow_all_comment) {
this.allow_all_comment = allow_all_comment;
}
public String getAvatar_large() {
return avatar_large;
}
public void setAvatar_large(String avatar_large) {
this.avatar_large = avatar_large;
}
public String getAvatar_hd() {
return avatar_hd;
}
public void setAvatar_hd(String avatar_hd) {
this.avatar_hd = avatar_hd;
}
public int getOnline_status() {
return online_status;
}
public void setOnline_status(int online_status) {
this.online_status = online_status;
}
public int getBi_followers_count() {
return bi_followers_count;
}
public void setBi_followers_count(int bi_followers_count) {
this.bi_followers_count = bi_followers_count;
}
/**
* 性别,m:男、f:女、n:未知
*/
private String gender;
/**
* 粉丝数
*/
private int followers_count;
/**
* 关注数
*/
private int friends_count;
/**
* 收藏数
*/
private int favourites_count;
/**
* 用户创建(注册)时间
*/
private String created_at;
/**
* 是否允许标识用户的地理位置,true:是,false:否
*/
private boolean geo_enabled;
/**
* 是否允许所有人对我的微博进行评论,true:是,false:否
*/
private boolean allow_all_comment;
/**
* 用户头像地址(大图),180×180像素
*/
private String avatar_large;
/**
* 用户头像地址(高清),高清头像原图
*/
private String avatar_hd;
/**
* 用户的在线状态,0:不在线、1:在线
*/
private int online_status;
}
这两个类我只写了对我有用的字段。
主要就这些了。