fastjson解析json数组
1、fastjson解析json数组(直接上代码)
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
ity.weix.OtWeiBatchgetMaterial;
articlesummary;
public class testTjtw {
public static void main(String[] args) throws Exception {
testTjtw tj = new testTjtw();
String jsonStr = "{\"item\":[" +
"{\"media_id\":\"DjkC7IK8dPuF6LkHXdpM7bGRF_ae0dk0ZG_0XIi-rvo\","+
"\"content\":{\"news_item\":["+
"{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
"\"content\":\"neirong\","+
"\"content_source_url\":\"ouyangyu\","+
"\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
"\"show_cover_pic\":0,"+
"\"url\":\"url\","+
"\"thumb_url\":\"url\","+
"\"need_open_comment\":1,"+
"\"only_fans_can_comment\":0"+
"},"+
"{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
"\"content\":\"neirong\","+
"\"content_source_url\":\"ouyangyu\","+
"\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
"\"show_cover_pic\":0,"+
"\"url\":\"url\","+
"\"thumb_url\":\"url\","+
"\"need_open_comment\":1,"+
"\"only_fans_can_comment\":0"+
"}"+
"],"+
"\"create_time\":1526465140,"+
"\"update_time\":1526465513"+
"},"+
"\"update_time\":1526465513"+
"},"+
"{\"media_id\":\"DjkC7IK8dPuF6LkHXdpM7T_W4zTBQZtLsxEzFW1c2iE\","+
"\"content\":{\"news_item\":["+
"{\"title\":\"1\",\"author\":\"欧阳宇\",\"digest\":\"1\","+
"\"content\":\"neirong\","+
"\"content_source_url\":\"ouyangyu\","+
"\"thumb_media_id\":\"DjkC7IK8dPuF6LkHXdpM7S6ApFtUuIYUXJ9bgr4KTuA\","+
"\"show_cover_pic\":0,"+
"\"url\":\"url\","+
"\"thumb_url\":\"url\","+
"\"need_open_comment\":1,"+
"\"only_fans_can_comment\":0"+
"}"+
"],"+
"\"create_time\":1526465140,"+
"\"update_time\":1526465513"+
"},"+
"\"update_time\":1526465513"+
"}"+
"],"+
"\"total_count\":4," +
"\"item_count\":4"+
"}";
String jsonStr2 = "{\"list\":["+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330874_1\",\"title\":\"fdsa\","+
"\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,"+
"\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_1\",\"title\":\"1\","+
"\"int_page_read_user\":1,\"int_page_read_count\":2,\"ori_page_read_user\":1,\"ori_page_read_count\":1,"+
"\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_2\",\"title\":\"2\","+
"\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":1,\"ori_page_read_count\":1,"+
"\"share_user\":0,\"share_count\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_3\",\"title\":\"3\",\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,\"share_user\":0,\"share_count\" "{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_4\",\"title\":\"4\",\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,\"share_user\":0,\"share_count\" "{\"ref_date\":\"2018-05-16\",\"user_source\":0,\"msgid\":\"2455330883_5\",\"title\":\"5\",\"int_page_read_user\":1,\"int_page_read_count\":1,\"ori_page_read_user\":0,\"ori_page_read_count\":0,\"share_user\":0,\"share_count\" "]"+
"}";
System.out.println("jsonStr:"+jsonStr);
tj.jsonToObject(jsonStr);
tj.jsonToObject1(jsonStr2);
}
//获取图⽂发每⽇数据(getarticlesummary)
fastjson字符串转数组public List<getarticlesummary> jsonToObject1(String jsonStr)throws Exception{
List<getarticlesummary> gtsys = new ArrayList<getarticlesummary>();
getarticlesummary gtsy = null;
JSONObject json = JSONObject.parseObject(jsonStr);
//获取item,得到json数组
JSONArray array = JSONArray("list");
for (int i = 0; i < array.size(); i++) {
JSONObject jo = JSONObject(i);
String ref_date = jo.getString("ref_date");
String msgid = jo.getString("msgid");
String title = jo.getString("title");
Integer int_page_read_user = jo.getInteger("int_page_read_user");
Integer int_page_read_count = jo.getInteger("int_page_read_count");
Integer ori_page_read_user = jo.getInteger("ori_page_read_user");
Integer ori_page_read_count = jo.getInteger("ori_page_read_count");
Integer share_user = jo.getInteger("share_user");
Integer share_count = jo.getInteger("share_count");
Integer add_to_fav_user = jo.getInteger("add_to_fav_user");
Integer add_to_fav_count = jo.getInteger("add_to_fav_count");
//将图⽂发每⽇数据json保存在集合中
gtsy = new getarticlesummary();
gtsy.setRef_date(ref_date);
gtsy.setMsgid(msgid);
gtsy.setTitle(title);
gtsy.setInt_page_read_user(int_page_read_user);
gtsy.setInt_page_read_count(int_page_read_count);
gtsy.setOri_page_read_user(ori_page_read_user);
gtsy.setOri_page_read_count(ori_page_read_count);
gtsy.setShare_user(share_user);
gtsy.setShare_count(share_count);
gtsy.setAdd_to_fav_user(add_to_fav_user);
gtsy.setAdd_to_fav_count(add_to_fav_count);
gtsys.add(gtsy);
}
System.out.(0).toString());
return gtsys;
}
//将json字符串数组解析成java对象
//获取图⽂素材
public List<OtWeiBatchgetMaterial> jsonToObject(String jsonStr)throws Exception{
List<OtWeiBatchgetMaterial> obms = new ArrayList<OtWeiBatchgetMaterial>();
OtWeiBatchgetMaterial obm = null;
JSONObject json = JSONObject.parseObject(jsonStr);
//获取item,得到json数组
JSONArray array = JSONArray("item");
for (int i = 0; i < array.size(); i++) {
JSONObject jo = JSONObject(i);
String media_id = jo.getString("media_id");
String content = jo.getString("content");
//将content转换为json对象
JSONObject json2 = JSONObject.parseObject(content);
//获取news_item得到json数组
JSONArray array2 = JSONArray("news_item");
for (int j = 0; j < array2.size(); j++) {
JSONObject jo2 = JSONObject(j);
String title = String("title");
String author = String("author");
String digest = String("digest");
String content2 = String("content");
String content_source_url = String("content_source_url");
String thumb_media_id = String("thumb_media_id");
Integer show_cover_pic = Integer("show_cover_pic");
String url = String("url");
String thumb_url = String("thumb_url");
Integer need_open_comment = Integer("need_open_comment");
Integer only_fans_can_comment = Integer("only_fans_can_comment"); String twCreate_time = String("create_time");
String twUpdate_time = String("update_time");
obm = new OtWeiBatchgetMaterial();
obm.setMedia_id(media_id);
obm.setTitle(title);
obm.setAuthor(author);
obm.setDigest(digest);
obm.setContent(content2);
obm.setContent_source_url(content_source_url);
obm.setThumb_media_id(thumb_media_id);
obm.setShow_cover_pic(show_cover_pic);
obm.setUrl(url);
obm.setThumb_url(thumb_url);
obm.setNeed_open_comment(need_open_comment);
obm.setOnly_fans_can_comment(only_fans_can_comment);
obm.setTwCreate_time(twCreate_time);
obm.setTwUpdate_time(twUpdate_time);
obms.add(obm);
}
}
System.out.(0).toString());
return obms;
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论