fastjson解析json数组
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;
}
}
fastjson解析json数组的更多相关文章
- JavaWeb_(Jar)使用fastjson解析json和序列化对象
菜鸟教程 传送门 JSON官网 传送门 fastjson插件下载 传送门 序列化[百度百科]:序列化 (Serialization)是将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对 ...
- FastJson解析Json,封装JavaBean对象
获取到前端的Json,后台对应封装JavaBean对象,对其解析赋值 获取到前端的json,对其进行分析 1.获取最外层前端json对应得JavaBean (1)未分析格式的json串 (2)初步格式 ...
- Scala中使用fastJson 解析json字符串
Scala中使用fastJson 解析json字符串 添加依赖 2.解析json字符 2.1可以通过JSON中的parseObject方法,把json字符转转换为一个JSONObject对象 2.2然 ...
- 使用QtScript库解析Json数组例子
本文转载自:http://blog.sina.com.cn/s/blog_671732440100uwxh.html 使用qtscipt库解析json数组首先在工程文件中加 QT += ...
- 解析json数组
解析json数组 JSONArray jsonArray = new JSONArray(markingStr); int iSize = jsonArray.length(); for (int i ...
- Gson解析Json数组
需求:从steam官网获取英雄数据,即为Json数据,并导入到本地数据库 Json数据是这样的 { "result": { "heroes": [ { &quo ...
- Spring Boot返回json数据及完美使用FastJson解析Json数据
Spring Boot返回json数据 视频地址:http://www.iqiyi.com/w_19rubxzsr5.html 博文参考:https://blog.csdn.net/linxingl ...
- Jquery解析json数组字符串
最近在工作中用到了Jquery来解析json字符串,网上解析jquery解析json单个对象的实例不少,但是jquery解析json数组的实例却是不多,下面我举一个简单的例子来跟大家分享与一下,本人水 ...
- java解析json数组
java解析json数组 import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; ...
随机推荐
- div设置滚动条内容任然显示不全
<div style="overflow:scroll;height:100%"></div> 注意:高度需设置为100%,不然内容显示不全
- Python开发【第三篇】:分支循环
1. if 条件语句 语法: if 条件: 代码块 # 条件为真执行 else: # else 可选 代码块 # 条件为假执行 示例: n = int(input('请输入一个数字:')) i ...
- C++内存管理之unique_ptr
一个unique_ptr"拥有“他所指向的对象.与shared_ptr不同,某个时刻只能有一个unique_ptr指向一个给定的对象.当unique_ptr被销毁时,它所指向的对象也被销毁. ...
- FUI- 我离钢铁侠还差几步?
本文来自网易云社区 作者:马宝 什么是FUI本文不累赘的可以自行Google,喜欢科幻的同学们都看一张图就能感受到FUI的魅力. 本文算是一篇所见即所的,可边学边干的原创教程.总结全文就一句话,&qu ...
- Java之Spring Boot详解(非原创)
文章大纲 一.Spring Boot 概述二.Spring Boot 入门案例三.Spring Boot核心功能代码实战四.项目源码与资料下载五.参考文章 一.Spring Boot 概述 1. ...
- Go:定时执行任务time.sleep和time.tick的优劣
golang 写循环执行的定时任务,常见的有以下三种实现方式:1.time.Sleep方法: for { time.Sleep(time.Second) fmt.Println("我在定时执 ...
- 在eclipse中打开文件所在的目录
eclipse中默认是不能直接打开文件所在的目录的,需要在文件中右键-->properties-->location,复制到资源管理器中才能打开文件所在的目录.这种方法很麻烦.这里介绍一种 ...
- Mysql常见问题集锦
缺少libstdc++.so.6库的原因及解决办法 https://blog.csdn.net/u010417185/article/details/69951312 https://www.cnbl ...
- having - 函数输出限制
引用:https://zhidao.baidu.com/question/406745181.html 对函数输出进行限制 栗子: 比如,我们可能只希望看到Store_Information数据表中销 ...
- Luogu P2290 [HNOI2004]树的计数 Prufer序列+组合数
最近碰了$prufer$ 序列和组合数..于是老师留了一道题:P2624 [HNOI2008]明明的烦恼 qwq要用高精... 于是我们有了弱化版:P2290 [HNOI2004]树的计数(考一样的可 ...