fastjson解析json数组

时间:2023-03-08 16:47:18

1、fastjson解析json数组(直接上代码)

import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.montnets.ott.entity.weix.OtWeiBatchgetMaterial;
import com.montnets.ott.entity.weix.getarticlesummary; 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\":\"http://ouyangyu.com\","+
"\"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\":\"http://ouyangyu.com\","+
"\"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\":\"http://ouyangyu.com\","+
"\"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\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"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\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0},"+
"{\"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\":0,\"add_to_fav_user\":0,\"add_to_fav_count\":0}"+
"]"+
"}"; System.out.println("jsonStr:"+jsonStr); tj.jsonToObject(jsonStr); tj.jsonToObject1(jsonStr2);
} //获取图文群发每日数据(getarticlesummary)
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 = json.getJSONArray("list"); for (int i = 0; i < array.size(); i++) {
JSONObject jo = array.getJSONObject(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.println(gtsys.get(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 = json.getJSONArray("item"); for (int i = 0; i < array.size(); i++) {
JSONObject jo = array.getJSONObject(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 = json2.getJSONArray("news_item");
for (int j = 0; j < array2.size(); j++) {
JSONObject jo2 = array2.getJSONObject(j);
String title = jo2.getString("title");
String author = jo2.getString("author");
String digest = jo2.getString("digest");
String content2 = jo2.getString("content");
String content_source_url = jo2.getString("content_source_url");
String thumb_media_id = jo2.getString("thumb_media_id");
Integer show_cover_pic = jo2.getInteger("show_cover_pic");
String url = jo2.getString("url");
String thumb_url = jo2.getString("thumb_url");
Integer need_open_comment = jo2.getInteger("need_open_comment");
Integer only_fans_can_comment = jo2.getInteger("only_fans_can_comment");
String twCreate_time = jo2.getString("create_time");
String twUpdate_time = jo2.getString("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.println(obms.get(0).toString());
return obms;
}
}