json 解析,

数据源:
json串 data
{“code”:20000,“message”:“OK”,“data”:{“items”:[{“author”:{“avatar”:“https://wpimg.wallstcn.com/bdf4d293-4f40-4615-b95e-9cb694409690",“display_name”:“选股宝盘中突发”,“id”:120000002047,“uri”:“https://wallstreetcn.com/authors/120000002047”},“categories”:[“china”,“economy”,“enterprise”,“shares”,“a-shares”],“comment_count”:0,“content_short”:"** 本文为选股宝最新上线【A股盘中突发】栏目,每个交易日盘中滚动发布,挖掘最新的重要题材机会,帮你第一时间潜伏热点。欢迎包时段订阅。",“display_time”:1526533196,“external”:false,“id”:3313795,“image_uri”:“https://wpimg.wallstcn.com/810efc67-ae3b-4446-b4e9-1911f23706bd.jpg",“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:true,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”],“related_topics”:[{“author_short_intro”:"",“id”:1000894,“image”:{“horizontal_image_uri”:"",“vertical_image_uri”:""},“is_bind_physical_product”:false,“latest_article_title”:"",“most_recent_content_time”:0,“sub_title”:"",“summary”:"",“title”:“A股盘中突发”,“uri”:""}],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:“联手工信部!国内首个政企合作大数据金融科技实验室成立,背后一家A股公司现身——5月17日A股盘中突发”,“unshow_content_short”:false,“uri”:“https://wallstreetcn.com/premium/articles/3313795?layout=wscn-layout”},{“author”:{“avatar”:“https://wpimg.wallstcn.com/365c616b-79d8-4f08-9509-05bc8d38f1b9”,“display_name”:“乐鸣”,“id”:100001460703,“uri”:“https://wallstreetcn.com/authors/100001460703”},“categories”:[“global”,“us”,“economy”,“bonds”,“shares”,“forex”],“comment_count”:0,“content_short”:“做空美债、做多美股和做空美元已成三大最拥挤交易,三大交易背后分别站着美联储、索罗斯和新债王,“巅峰对决”谁将是最后赢家?”,“display_time”:1526527081,“external”:false,“id”:3313344,“image_uri”:“https://wpimg.wallstcn.com/37a37720-94d4-443d-835c-033d02e8c715.jpg”,“is_in_vip_privilege”:true,“is_paid”:false,“is_priced”:true,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”],“related_topics”:[{“author_short_intro”:"",“id”:1000023,“image”:{“horizontal_image_uri”:"",“vertical_image_uri”:""},“is_bind_physical_product”:false,“latest_article_title”:"",“most_recent_content_time”:0,“sub_title”:"",“summary”:"",“title”:“见闻主编精选”,“uri”:""}],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:"大特写 | 三大最拥挤交易 美联储、索罗斯和新债王的“巅峰对决””,“unshow_content_short”:false,“uri”:"https://wallstreetcn.com/premium/articles/3313344?layout=wscn-layout",“vip_uri”:“https://wallstreetcn.com/vip/articles/3313344”},{“author”:{“avatar”:“https://wpimg.wallstcn.com/bdf4d293-4f40-4615-b95e-9cb694409690”,“display_name”:“选股宝盘中突发”,“id”:120000002047,“uri”:“https://wallstreetcn.com/authors/120000002047”},“categories”:[“china”,“enterprise”,“shares”,“economy”,“a-shares”],“comment_count”:0,“content_short”:" 本文为选股宝最新上线【A股盘中突发】栏目,每个交易日盘中滚动发布,挖掘最新的重要题材机会,帮你第一时间潜伏热点。欢迎包时段订阅。",“display_time”:1526525111,“external”:false,“id”:3313727,“image_uri”:“https://wpimg.wallstcn.com/b3100fc1-19fa-4d17-b05b-633926a7dbdb.png",“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:true,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”],“related_topics”:[{“author_short_intro”:"",“id”:1000894,“image”:{“horizontal_image_uri”:"",“vertical_image_uri”:""},“is_bind_physical_product”:false,“latest_article_title”:"",“most_recent_content_time”:0,“sub_title”:"",“summary”:"",“title”:“A股盘中突发”,“uri”:""}],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:“刚刚!官方公布了这份物联网设施建设时间表,提前2年完成大超预期,这家A股公司已获首张“入场券”——5月17日A股盘中突发”,“unshow_content_short”:false,“uri”:“https://wallstreetcn.com/premium/articles/3313727?layout=wscn-layout”},{“author”:{“avatar”:“https://wpimg.wallstcn.com/affa008e-b43f-449a-b5ef-7471bf5d6b89”,“display_name”:“曾心怡”,“id”:120000002173,“uri”:“https://wallstreetcn.com/authors/120000002173”},“categories”:[“global”,“us”,“enterprise”,“bonds”,“shares”],“comment_count”:0,“content_short”:“一季度,索罗斯买入3500万美元特斯拉可转换债券,这部分资金或正是在特斯拉三月底跌至谷底时涌入。分析认为,此举或将短暂解决特斯拉的现金压力。而对于索罗斯而言,无论特斯拉股价升跌,这都绝不是赔本的生意。”,“display_time”:1526521960,“external”:false,“id”:3313640,“image_uri”:“https://wpimg.wallstcn.com/ca226656-db0e-453f-9bcf-4013a7c178fc.jpg”,“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:false,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”],“related_topics”:[],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:"二度携手 索罗斯“抄底”马斯克”,“unshow_content_short”:false,“uri”:“https://wallstreetcn.com/articles/3313640"},{“author”:{“avatar”:“https://wpimg.wallstcn.com/b8d91635-d0df-436b-809c-f55e8bb45fd4”,“display_name”:“刘镔练”,“id”:120000001482,“uri”:“https://wallstreetcn.com/authors/120000001482”},“categories”:[“global”,“china”,“enterprise”,“economy”,“shares”,“tmt-firm”],“comment_count”:0,“content_short”:“腾讯第一季度净利润与总营收均超市场预期,虽然non-GAAP净利润不及各大机构预期,但一季度网络游戏收入287.78亿元人民币,环比重拾涨势,实现14.4%的增幅。”,“display_time”:1526519902,“external”:false,“id”:3313059,“image_uri”:“https://wpimg.wallstcn.com/6029352f-27b0-458a-92da-50c06a169db1.jpg”,“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:false,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”,“tech-platform”],“related_topics”:[],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:"腾讯一季报超预期 游戏业务环比重拾涨势 股价开盘涨7%”,“unshow_content_short”:false,“uri”:"https://wallstreetcn.com/articles/3313059"},{“author”:{“avatar”:“https://wpimg.wallstcn.com/bdf4d293-4f40-4615-b95e-9cb694409690”,“display_name”:“选股宝盘中突发”,“id”:120000002047,“uri”:“https://wallstreetcn.com/authors/120000002047”},“categories”:[“china”,“enterprise”,“shares”,“economy”,“a-shares”],“comment_count”:0,“content_short”:" 本文为选股宝最新上线【A股盘中突发】栏目,每个交易日盘中滚动发布,挖掘最新的重要题材机会,帮你第一时间潜伏热点。欢迎包时段订阅。**”,“display_time”:1526519716,“external”:false,“id”:3313610,“image_uri”:“https://wpimg.wallstcn.com/8a8e4ef3-579c-4cd1-9b6d-91441d4c51ae.png",“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:true,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”],“related_topics”:[{“author_short_intro”:"",“id”:1000894,“image”:{“horizontal_image_uri”:"",“vertical_image_uri”:""},“is_bind_physical_product”:false,“latest_article_title”:"",“most_recent_content_time”:0,“sub_title”:"",“summary”:"",“title”:“A股盘中突发”,“uri”:""}],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:“卫星导航中国方案重磅发布,北斗主题迎来2.0时代,这两家A股公司已率先卡位——5月17日A股盘中突发”,“unshow_content_short”:false,“uri”:“https://wallstreetcn.com/premium/articles/3313610?layout=wscn-layout”},{“author”:{“avatar”:“https://wpimg.wallstcn.com/1761722f-7839-4306-b322-a14630ac2e7f”,“display_name”:“脱水研报”,“id”:120000000496,“uri”:“https://wallstreetcn.com/authors/120000000496”},“categories”:[“global”,“enterprise”,“china”,“economy”,“shares”,“a-shares”],“comment_count”:0,“content_short”:“1、两个进口替代概念叠身,且是实实在在的批量供货,新能源产业链上很小的细分龙头。\n2、拳头产品供不应求;另一产品量价齐升,最后还有市占率80%的核心医药资产待装入。\n3、湖南盐业四天三板引发食品狂潮,但这个细分领域成长性最优。\n4、行业一重磅政策7月1日起执行,逻辑类似供给侧改革。这家公司深度受益,早已做好准备,下半年业绩拐点确认。”,“display_time”:1526516740,“external”:false,“id”:3313188,“image_uri”:“https://wpimg.wallstcn.com/7bf78c05-dbee-4a4e-b58d-b08684232831.png”,“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:true,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”],“related_topics”:[{“author_short_intro”:"",“id”:1000318,“image”:{“horizontal_image_uri”:"",“vertical_image_uri”:""},“is_bind_physical_product”:false,“latest_article_title”:"",“most_recent_content_time”:0,“sub_title”:"",“summary”:"",“title”:“A股脱水研报”,“uri”:""}],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:“主业量价齐升,还有市占率80%的某医药核心资产注入;新材料小龙头,双进口替代加持——0516脱水研报”,“unshow_content_short”:false,“uri”:“https://wallstreetcn.com/premium/articles/3313188?layout=wscn-layout”},{“author”:{“avatar”:“https://wpimg.wallstcn.com/2651409d-2179-4ff2-a299-ec8affb49e84”,“display_name”:“杜玉”,“id”:120000000347,“uri”:“https://wallstreetcn.com/authors/120000000347”},“categories”:[“global”,“china”,“enterprise”,“shares”],“comment_count”:0,“content_short”:"网易一季度EPS 1.61美元,逊于预期的2.15美元;营收22.6亿美元,略高于预期的22亿美元。在线游戏营收88亿元人民币,同比下滑18.4%,电商净营收37亿元人民币,同比增长101.0%,盘后ADR股价跌近2%。”,“display_time”:1526512015,“external”:false,“id”:3313458,“image_uri”:“https://wpimg.wallstcn.com/5f4bae4c-4ffc-44e5-af92-f96135a652b8.jpg",“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:false,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”,“tech-platform”],“related_topics”:[],“source_name”:“杜玉、祁月”,“source_uri”:"",“symbols”:[],“tags”:[],“title”:"网易一季度EPS不及预期 游戏收入加速下跌电商收入大涨”,“unshow_content_short”:false,“uri”:“https://wallstreetcn.com/articles/3313458"},{“author”:{“avatar”:“https://dn-wscn-avatar.qbox.me//59/4d/7c/-logo.png”,“display_name”:“李丹”,“id”:75,“uri”:“https://wallstreetcn.com/authors/75”},“categories”:[“global”,“us”,“europe”,“shares”,“us-shares”,“bonds”,“china”,“forex”,“commodities”],“comment_count”:0,“content_short”:“梅西百货财报优于预期股价涨超10%。小盘股代表罗素2000指数收创新高。10年期美债收益率升至3.1%,本周再创近七年新高。欧元创五个月新低。10年期意债收益率创近11个月最大涨幅。”,“display_time”:1526509544,“external”:false,“id”:3313443,“image_uri”:“https://wpimg.wallstcn.com/23e71d8f-bac0-4b00-807d-91a07dcca4ba.jpg”,“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:false,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”],“related_topics”:[],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:"无惧美债收益率再创新高 美股重回涨势 意大利退欧危机引发欧元意债大跌”,“unshow_content_short”:false,“uri”:“https://wallstreetcn.com/articles/3313443"},{“author”:{“avatar”:“https://dn-wscn-avatar.qbox.me//59/4d/7c/-logo.png”,“display_name”:“李丹”,“id”:75,“uri”:“https://wallstreetcn.com/authors/75”},“categories”:[“global”,“china”,“us”,“shares”,“us-shares”,“internal”],“comment_count”:0,“content_short”:“本周一中国外交部发言人称,应美国政府邀请,刘鹤副总理将于5月15日至19日赴美访问,同美方经济团队继续就两国经贸问题进行磋商。”,“display_time”:1526499498,“external”:false,“id”:3313442,“image_uri”:“https://wpimg.wallstcn.com/57ef04a6-763b-481b-96e9-57c7d44569ca.jpg”,“is_in_vip_privilege”:false,“is_paid”:false,“is_priced”:false,“is_trial”:false,“layout”:“wscn-layout”,“pageviews”:0,“platforms”:[“wscn-platform”],“related_topics”:[],“source_name”:"",“source_uri”:"",“symbols”:[],“tags”:[],“title”:“媒体:鹰派白宫贸易顾问纳瓦罗被排除在中美磋商之外”,“unshow_content_short”:false,“uri”:“https://wallstreetcn.com/articles/3313442”}],“next_cursor”:"1528121707,1526499498”}}

首先我们需要把这个json串的数据结构和层级了解清楚,有几种方案
第一种方案,就是自己分析json的数据结构和层级,
第二种方案,利用json在线解析工具解析
大家肯定会选择后者,因为它的效率要比前者高的多,当然如果对json串比较敏感的人肯定会选第一种,因为不需要花时间去第三方解析了,那只是对于极个别群体。

了解清楚json的数据结构和层级之后,我们就可以解析json了
首先我们要接收json串通过 JSONObject 格式化一下,格式化一下json字符串的数据结构
String context = data(以上的数据源)
JSONObject jsonArray = (JSONObject) JSONObject.parse(context);
格式化之后我就需要拿json里面的各个层级的内容了,按照数据层级的顺序来
第一层 data
JSONObject jsonObject = (JSONObject) JSONObject.parse(jsonArray.getString(“data”));
如果是跟data并排的层级 还是在 jsonArray 里面取,但是我现在需要的数据在data里面
所以用同样的方式来取我要的数据:
JSONArray jsonObjectItems = JSONArray.fromObject(jsonObject.getString(“items”));
我们拿到了data之后,需要data 里面的数据,那么我们就需要解析data了,我这里创建了一个实体类,把我需要的信息保存下来,data的数据可能不止一条,so,我这里用上来循环来处理。
items 下面还有jsonObjectAuthor 是我需要的对象 ,我们拿到 jsonObjectAuthor 就可以正式解析了,come on。
循环的长度是 jsonObjectItems的长度。因为他是 jsonObjectAuthor 的父级
拿到 jsonObjectAuthor 对象之后,用getString()方法去对应的字段内容;,然后装到本地创建的对象属性中,装完对象后把对象添加到list集合中,我这里需要添加到数据库中,所以在这里添加了库数据的检索操作,(思路,在库中新建一张表,uid:json串中的id,唯一标识)用这个标识去库中检索是否存在相同的数据,然后根据结果另做处理。

for (int i = 0; i < jsonObjectItems.size(); i++) {
            Information information = new Information();
            JSONObject jsonObjectAuthor = (JSONObject)      JSONObject.parse(jsonObjectItems.getJSONObject(i).getString("author"));
            jsonObjectAuthor.getString("avatar");
            information.setDisplayName(jsonObjectAuthor.getString("display_name"));
            information.setSourceUri(jsonObjectAuthor.getString("uri"));
            JSONObject.parse(jsonObjectItems.getJSONObject(i).getString("author"));
            jsonObjectItems.getJSONObject(i).getString("comment_count");
            jsonObjectItems.getJSONObject(i).getString("content_short");
            jsonObjectItems.getJSONObject(i).getString("external");
            jsonObjectItems.getJSONObject(i).getString("id");
            jsonObjectItems.getJSONObject(i).getString("image_uri");
            jsonObjectItems.getJSONObject(i).getString("is_paid");
            jsonObjectItems.getJSONObject(i).getString("is_priced");
            jsonObjectItems.getJSONObject(i).getString("is_trial");
            jsonObjectItems.getJSONObject(i).getString("layout");
            jsonObjectItems.getJSONObject(i).getString("pageviews");
            jsonObjectItems.getJSONObject(i).getString("source_uri");
            //数组
            jsonObjectItems.getJSONObject(i).getString("platforms");
            //对象
            jsonObjectItems.getJSONObject(i).getString("related_topics");
            jsonObjectItems.getJSONObject(i).getString("source_name");
            jsonObjectItems.getJSONObject(i).getString("symbols");
            jsonObjectItems.getJSONObject(i).getString("tags");
            jsonObjectItems.getJSONObject(i).getString("unshow_content_short");
            //时间戳转换
            String displayTime = timeStamp2Date(jsonObjectItems.getJSONObject(i).getString("display_time"), "yyyy-MM-dd HH:mm:ss");
            information.setDisplayTime(Timestamp.valueOf(displayTime));
            information.setInfoId(jsonObjectItems.getJSONObject(i).getString("id"));
            information.setTitle(jsonObjectItems.getJSONObject(i).getString("title"));
            information.setUrl(jsonObjectItems.getJSONObject(i).getString("uri"));
            information.setDelFlag(0);
            information.setVersion(0);
            information.setCreateTime(new Timestamp(System.currentTimeMillis()));
             try {
                 if (null == informationDao.exitInformation(information.getInfoId())) {
                     informationDao.save(information);
                 }
             }catch (Exception e){
                 continue;
             }
            informationList.add(information);
        }
        ```
这段是 解析,数据库检索,添加数据的功能代码。

整体源码段:


    List<Information> informationList = new LinkedList<>();
    try {
     //第三方接口地址
        JSONObject jsonArray = (JSONObject) JSONObject.parse(context);
        JSONObject jsonObject = (JSONObject) JSONObject.parse(jsonArray.getString("data"));
        JSONArray jsonObjectItems = JSONArray.fromObject(jsonObject.getString("items"));
        
        for (int i = 0; i < jsonObjectItems.size(); i++) {
            Information information = new Information();
            JSONObject jsonObjectAuthor = (JSONObject) JSONObject.parse(jsonObjectItems.getJSONObject(i).getString("author"));
            //作者
            jsonObjectAuthor.getString("avatar");
            information.setDisplayName(jsonObjectAuthor.getString("display_name"));
            information.setSourceUri(jsonObjectAuthor.getString("uri"));
            //分类  数组
            JSONObject.parse(jsonObjectItems.getJSONObject(i).getString("author"));

            jsonObjectItems.getJSONObject(i).getString("comment_count");
            jsonObjectItems.getJSONObject(i).getString("content_short");
            jsonObjectItems.getJSONObject(i).getString("external");
            jsonObjectItems.getJSONObject(i).getString("id");
            jsonObjectItems.getJSONObject(i).getString("image_uri");
            jsonObjectItems.getJSONObject(i).getString("is_paid");
            jsonObjectItems.getJSONObject(i).getString("is_priced");
            jsonObjectItems.getJSONObject(i).getString("is_trial");
            jsonObjectItems.getJSONObject(i).getString("layout");
            jsonObjectItems.getJSONObject(i).getString("pageviews");
            jsonObjectItems.getJSONObject(i).getString("source_uri");
            //数组
            jsonObjectItems.getJSONObject(i).getString("platforms");
            //对象
            jsonObjectItems.getJSONObject(i).getString("related_topics");
            jsonObjectItems.getJSONObject(i).getString("source_name");
            jsonObjectItems.getJSONObject(i).getString("symbols");
            jsonObjectItems.getJSONObject(i).getString("tags");
            jsonObjectItems.getJSONObject(i).getString("unshow_content_short");
            //时间戳转换
            String displayTime = timeStamp2Date(jsonObjectItems.getJSONObject(i).getString("display_time"), "yyyy-MM-dd HH:mm:ss");
            information.setDisplayTime(Timestamp.valueOf(displayTime));
            information.setInfoId(jsonObjectItems.getJSONObject(i).getString("id"));
            information.setTitle(jsonObjectItems.getJSONObject(i).getString("title"));
            information.setUrl(jsonObjectItems.getJSONObject(i).getString("uri"));
            information.setDelFlag(0);
            information.setVersion(0);

            information.setCreateTime(new Timestamp(System.currentTimeMillis()));
             try {
                 if (null == informationDao.exitInformation(information.getInfoId())) {
                     informationDao.save(information);
                 }
             }catch (Exception e){
                 continue;
             }

            informationList.add(information);
        }
    } catch (Exception e) {

    }finally {
        //第三方接口无法正常访问 访问本地的最新资讯
        if(informationList==null){
            informationList = informationDao.find("from  Information  where delFlag = 0   order by displayTime desc limit 0,50");
        }
    }
}

/**
时间戳转换成日期格式字符串
@param seconds 精确到秒的字符串
@param formatStr
@return
*/
public static String timeStamp2Date(String seconds, String format) {
if (seconds == null || seconds.isEmpty() || seconds.equals(“null”)) {
return “”;
}
if (format == null || format.isEmpty()) {
format = “yyyy-MM-dd HH:mm:ss”;
}
SimpleDateFormat sdf = new SimpleDateFormat(format);
return sdf.format(new Date(Long.valueOf(seconds + “000”)));
}

猜你喜欢

转载自blog.csdn.net/qq_38637728/article/details/82896722
今日推荐