您的位置:首页 > Web前端 > JavaScript

fastjson进行复杂微博数据解析

2015-04-17 23:07 579 查看
微博获取的用户发微博json数据。我用阿里提供的 fastjson进行解析。

json数据格式如下:

{
"bi_followers_count": 0,
"domain": "",
"avatar_large": "http://tp1.sinaimg.cn/32300/180/0/0",
"ptype": 0,
"block_word": 0,
"star": 0,
"id": 32300,
"city": "9",
"verified": false,
"follow_me": false,
"verified_reason": "",
"followers_count": 11,
"location": "河北 沧州",
"mbtype": 0,
"profile_url": "uc32300",
"province": "13",
"avatar_hd": "http://tp1.sinaimg.cn/32300/180/0/0",
"statuses_count": 0,
"description": "",
"friends_count": 50,
"online_status": 0,
"mbrank": 0,
"allow_all_act_msg": false,
"profile_image_url": "http://tp1.sinaimg.cn/32300/50/0/0",
"idstr": "32300",
"allow_all_comment": true,
"geo_enabled": true,
"class": 1,
"name": "狮城夜猫201405",
"lang": "zh-cn",
"weihao": "",
"remark": "",
"favourites_count": 0,
"screen_name": "狮城夜猫201405",
"url": "",
"gender": "f",
"created_at": "Tue Feb 25 19:04:46 +0800 2014",
"verified_type": -1,
"following": false
}

{
"status": {
"reposts_count": 0,
"favorited": false,
"attitudes_count": 0,
"truncated": false,
"text": "转发微博",
"created_at": "Fri Mar 30 18:33:22 +0800 2012",
"mlevel": 0,
"visible": {
"type": 0,
"list_id": 0
},
"idstr": "3429347434629736",
"mid": "3429347434629736",
"source": "<a href=\"http://app.weibo.com/t/feed/5xCS0B\" rel=\"nofollow\">新浪微博手机版</a>",
"in_reply_to_status_id": "",
"in_reply_to_screen_name": "",
"in_reply_to_user_id": "",
"comments_count": 0,
"geo": null,
"id": 3429347434629736,
"pic_urls": []
},
"domain": "uc26776400",
"avatar_large": "http://tp1.sinaimg.cn/26776400/180/5626488822/1",
"bi_followers_count": 0,
"ptype": 0,
"block_word": 0,
"star": 0,
"id": 26776400,
"city": "6",
"verified": false,
"follow_me": false,
"verified_reason": "",
"followers_count": 12,
"location": "河北 保定",
"mbtype": 0,
"profile_url": "uc26776400",
"province": "13",
"avatar_hd": "http://tp1.sinaimg.cn/26776400/180/5626488822/1",
"statuses_count": 5,
"description": "",
"friends_count": 154,
"online_status": 0,
"mbrank": 0,
"allow_all_act_msg": false,
"profile_image_url": "http://tp1.sinaimg.cn/26776400/50/5626488822/1",
"idstr": "26776400",
"allow_all_comment": true,
"geo_enabled": true,
"class": 1,
"name": "我自大",
"lang": "zh-cn",
"weihao": "",
"remark": "",
"favourites_count": 0,
"screen_name": "我自大",
"url": "",
"gender": "m",
"created_at": "Tue Nov 10 00:34:28 +0800 2009",
"verified_type": -1,
"following": false
}

主要就是对其中的逻辑关系进行分析。[ ] 就是进行数组解析。
package com.Jason.test;

import java.util.ArrayList;
import java.util.List;

public class Status {
private Long reposts_count;
private Boolean favorited;
private Long attitudes_count;
private String original_pic;
private Boolean truncated;
private String thumbnail_pic;
private String text;
private String created_at;
private Long mlevel;
private Visible visible;
private String idstr;
private String mid;
private String source;
private String in_reply_to_status_id;
private String in_reply_to_screen_name;
private String in_reply_to_user_id;
private String bmiddle_pic;
private String comments_count;
private String geo;
private String id;
List<Pic_urls> pic_urls = new ArrayList<Pic_urls>();

public List<Pic_urls> getPic_urls() {
return pic_urls;
}
public void setPic_urls(List<Pic_urls> pic_urls) {
this.pic_urls = pic_urls;
}
public Long getReposts_count() {
return reposts_count;
}
public void setReposts_count(Long reposts_count) {
this.reposts_count = reposts_count;
}
public Boolean getFavorited() {
return favorited;
}
public void setFavorited(Boolean favorited) {
this.favorited = favorited;
}
public Long getAttitudes_count() {
return attitudes_count;
}
public void setAttitudes_count(Long attitudes_count) {
this.attitudes_count = attitudes_count;
}
public String getOriginal_pic() {
return original_pic;
}
public void setOriginal_pic(String original_pic) {
this.original_pic = original_pic;
}
public Boolean getTruncated() {
return truncated;
}
public void setTruncated(Boolean truncated) {
this.truncated = truncated;
}
public String getThumbnail_pic() {
return thumbnail_pic;
}
public void setThumbnail_pic(String thumbnail_pic) {
this.thumbnail_pic = thumbnail_pic;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getCreated_at() {
return created_at;
}
public void setCreated_at(String created_at) {
this.created_at = created_at;
}
public Long getMlevel() {
return mlevel;
}
public void setMlevel(Long mlevel) {
this.mlevel = mlevel;
}
public Visible getVisible() {
return visible;
}
public void setVisible(Visible visible) {
this.visible = visible;
}
public String getIdstr() {
return idstr;
}
public void setIdstr(String idstr) {
this.idstr = idstr;
}
public String getMid() {
return mid;
}
public void setMid(String mid) {
this.mid = mid;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getIn_reply_to_status_id() {
return in_reply_to_status_id;
}
public void setIn_reply_to_status_id(String in_reply_to_status_id) {
this.in_reply_to_status_id = in_reply_to_status_id;
}
public String getIn_reply_to_screen_name() {
return in_reply_to_screen_name;
}
public void setIn_reply_to_screen_name(String in_reply_to_screen_name) {
this.in_reply_to_screen_name = in_reply_to_screen_name;
}
public String getIn_reply_to_user_id() {
return in_reply_to_user_id;
}
public void setIn_reply_to_user_id(String in_reply_to_user_id) {
this.in_reply_to_user_id = in_reply_to_user_id;
}
public String getBmiddle_pic() {
return bmiddle_pic;
}
public void setBmiddle_pic(String bmiddle_pic) {
this.bmiddle_pic = bmiddle_pic;
}
public String getComments_count() {
return comments_count;
}
public void setComments_count(String comments_count) {
this.comments_count = comments_count;
}
public String getGeo() {
return geo;
}
public void setGeo(String geo) {
this.geo = geo;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}

package com.Jason.test;

public class Visible {
private String type;
private String list_id;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getList_id() {
return list_id;
}

public void setList_id(String list_id) {
this.list_id = list_id;
}
}

package com.Jason.test;

public class Pic_urls {
private String thumbnail_pic;

public String getThumbnail_pic() {
return thumbnail_pic;
}

public void setThumbnail_pic(String thumbnail_pic) {
this.thumbnail_pic = thumbnail_pic;
}
}
package com.Jason.test;

public class Result {
private Status status;
private String domain;
private String avatar_large;
private Long bi_followers_count;
private String ptype;
private String block_word;
private Long star;
private String id;
private Long city;
private Boolean verified;
private Boolean follow_me;
private String verified_reason;
private Long followers_count;
private String location;
private String mbtype;
private String profile_url;
private Long province;
private String avatar_hd;
private Long statuses_count;
private String description;
private Long friends_count;
private String online_status;
private String mbrank;
private Boolean allow_all_act_msg;
private String profile_image_url;
private String idstr;
private Boolean allow_all_comment;
private Boolean geo_enabled;
//private String class;
private String lang;
private String weihao;
private String remark;
private Long favourites_count;
private String screen_name;
private String url;
private String gender;
private String created_at;
private String verified_type;
private Boolean following;

public Status getStatus() {
return status;
}

public void setStatus(Status status) {
this.status = status;
}

public String getDomain() {
return domain;
}

public void setDomain(String domain) {
this.domain = domain;
}

public String getAvatar_large() {
return avatar_large;
}

public void setAvatar_large(String avatar_large) {
this.avatar_large = avatar_large;
}

public Long getBi_followers_count() {
return bi_followers_count;
}

public void setBi_followers_count(Long bi_followers_count) {
this.bi_followers_count = bi_followers_count;
}

public String getPtype() {
return ptype;
}

public void setPtype(String ptype) {
this.ptype = ptype;
}

public String getBlock_word() {
return block_word;
}

public void setBlock_word(String block_word) {
this.block_word = block_word;
}

public Long getStar() {
return star;
}

public void setStar(Long star) {
this.star = star;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public Long getCity() {
return city;
}

public void setCity(Long city) {
this.city = city;
}

public Boolean getVerified() {
return verified;
}

public void setVerified(Boolean verified) {
this.verified = verified;
}

public Boolean getFollow_me() {
return follow_me;
}

public void setFollow_me(Boolean follow_me) {
this.follow_me = follow_me;
}

public String getVerified_reason() {
return verified_reason;
}

public void setVerified_reason(String verified_reason) {
this.verified_reason = verified_reason;
}

public Long getFollowers_count() {
return followers_count;
}

public void setFollowers_count(Long followers_count) {
this.followers_count = followers_count;
}

public String getLocation() {
return location;
}

public void setLocation(String location) {
this.location = location;
}

public String getMbtype() {
return mbtype;
}

public void setMbtype(String mbtype) {
this.mbtype = mbtype;
}

public Long getProvince() {
return province;
}

public void setProvince(Long province) {
this.province = province;
}

public String getProfile_url() {
return profile_url;
}

public void setProfile_url(String profile_url) {
this.profile_url = profile_url;
}

public String getAvatar_hd() {
return avatar_hd;
}

public void setAvatar_hd(String avatar_hd) {
this.avatar_hd = avatar_hd;
}

public Long getStatuses_count() {
return statuses_count;
}

public void setStatuses_count(Long statuses_count) {
this.statuses_count = statuses_count;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Long getFriends_count() {
return friends_count;
}

public void setFriends_count(Long friends_count) {
this.friends_count = friends_count;
}

public String getOnline_status() {
return online_status;
}

public void setOnline_status(String online_status) {
this.online_status = online_status;
}

public String getMbrank() {
return mbrank;
}

public void setMbrank(String mbrank) {
this.mbrank = mbrank;
}

public Boolean getAllow_all_act_msg() {
return allow_all_act_msg;
}

public void setAllow_all_act_msg(Boolean allow_all_act_msg) {
this.allow_all_act_msg = allow_all_act_msg;
}

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 getIdstr() {
return idstr;
}

public void setIdstr(String idstr) {
this.idstr = idstr;
}

public Boolean getAllow_all_comment() {
return allow_all_comment;
}

public void setAllow_all_comment(Boolean allow_all_comment) {
this.allow_all_comment = allow_all_comment;
}

public Boolean getGeo_enabled() {
return geo_enabled;
}

public void setGeo_enabled(Boolean geo_enabled) {
this.geo_enabled = geo_enabled;
}

public String getWeihao() {
return weihao;
}

public void setWeihao(String weihao) {
this.weihao = weihao;
}

public String getLang() {
return lang;
}

public void setLang(String lang) {
this.lang = lang;
}

public String getRemark() {
return remark;
}

public void setRemark(String remark) {
this.remark = remark;
}

public Long getFavourites_count() {
return favourites_count;
}

public void setFavourites_count(Long favourites_count) {
this.favourites_count = favourites_count;
}

public String getScreen_name() {
return screen_name;
}

public void setScreen_name(String screen_name) {
this.screen_name = screen_name;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getCreated_at() {
return created_at;
}

public void setCreated_at(String created_at) {
this.created_at = created_at;
}

public String getVerified_type() {
return verified_type;
}

public void setVerified_type(String verified_type) {
this.verified_type = verified_type;
}

public Boolean getFollowing() {
return following;
}

public void setFollowing(Boolean following) {
this.following = following;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}

package com.Jason.test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.alibaba.fastjson.JSON;

public class Test {
public Test(){
String json = readFile("files/user-c-part.json");
System.out.println(json);
Result result = JSON.parseObject(json, Result.class);
System.out.println(JSON.toJSONString(result));

}

public String readFile(String file){
try {
FileInputStream fis = new FileInputStream(file);
StringBuffer sb = new StringBuffer();
int len = 0;
while(true){
byte[] b = new byte[10000];
len = fis.read(b);
if(len<0){
break;
}
sb.append(new String(b,0, len));
}

return sb.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

/**
* @param args
*/
public static void main(String[] args) {
new Test();
}
}


最后奉上解析结果
{"status": {"reposts_count": 0, "favorited": false, "attitudes_count": 0, "truncated": false, "text": "\u8f6c\u53d1\u5fae\u535a", "created_at": "Fri Mar 30 18:33:22 +0800 2012", "mlevel": 0, "visible": {"type": 0, "list_id": 0}, "idstr": "3429347434629736", "mid": "3429347434629736", "source": "<a href=\"http://app.weibo.com/t/feed/5xCS0B\" rel=\"nofollow\">\u65b0\u6d6a\u5fae\u535a\u624b\u673a\u7248</a>", "in_reply_to_status_id": "", "in_reply_to_screen_name": "", "in_reply_to_user_id": "", "comments_count": 0, "geo": null, "id": 3429347434629736, "pic_urls": []}, "domain": "uc26776400", "avatar_large": "http://tp1.sinaimg.cn/26776400/180/5626488822/1", "bi_followers_count": 0, "ptype": 0, "block_word": 0, "star": 0, "id": 26776400, "city": "6", "verified": false, "follow_me": false, "verified_reason": "", "followers_count": 12, "location": "\u6cb3\u5317 \u4fdd\u5b9a", "mbtype": 0, "profile_url": "uc26776400", "province": "13", "avatar_hd": "http://tp1.sinaimg.cn/26776400/180/5626488822/1", "statuses_count": 5, "description": "", "friends_count": 154, "online_status": 0, "mbrank": 0, "allow_all_act_msg": false, "profile_image_url": "http://tp1.sinaimg.cn/26776400/50/5626488822/1", "idstr": "26776400", "allow_all_comment": true, "geo_enabled": true, "class": 1, "name": "\u6211\u81ea\u5927", "lang": "zh-cn", "weihao": "", "remark": "", "favourites_count": 0, "screen_name": "\u6211\u81ea\u5927", "url": "", "gender": "m", "created_at": "Tue Nov 10 00:34:28 +0800 2009", "verified_type": -1, "following": false}

{"allow_all_act_msg":false,"allow_all_comment":true,"avatar_hd":"http://tp1.sinaimg.cn/26776400/180/5626488822/1","avatar_large":"http://tp1.sinaimg.cn/26776400/180/5626488822/1","bi_followers_count":0,"block_word":"0","city":6,"created_at":"Tue Nov 10 00:34:28 +0800 2009","description":"","domain":"uc26776400","favourites_count":0,"follow_me":false,"followers_count":12,"following":false,"friends_count":154,"gender":"m","geo_enabled":true,"id":"26776400","idstr":"26776400","lang":"zh-cn","location":"河北 保定","mbrank":"0","mbtype":"0","online_status":"0","profile_image_url":"http://tp1.sinaimg.cn/26776400/50/5626488822/1","profile_url":"uc26776400","province":13,"ptype":"0","remark":"","screen_name":"我自大","star":0,"status":{"attitudes_count":0,"comments_count":"0","created_at":"Fri Mar 30 18:33:22 +0800 2012","favorited":false,"id":"3429347434629736","idstr":"3429347434629736","in_reply_to_screen_name":"","in_reply_to_status_id":"","in_reply_to_user_id":"","mid":"3429347434629736","mlevel":0,"pic_urls":[],"reposts_count":0,"source":"<a href=\"http://app.weibo.com/t/feed/5xCS0B\" rel=\"nofollow\">新浪微博手机版</a>","text":"转发微博","truncated":false,"visible":{"list_id":"0","type":"0"}},"statuses_count":5,"url":"","verified":false,"verified_reason":"","verified_type":"-1","weihao":""}


我用到的fastjson上传到百度云供大家使用
"C:\tools\EXJar\fastjson-1.2.5.jar"

"C:\tools\EXJar\fastjson-1.2.5-sources.jar"

链接: http://pan.baidu.com/s/1bnAStpd 密码: pmb2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息