加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

FastJson 常API用

发布时间:2020-12-16 18:55:42 所属栏目:百科 来源:网络整理
导读:astjson 是一个性能极好的用 Java 语言实现的 JSON 解析器和生成器,来自 阿里巴巴的工程师开发。 主要特点: 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson) 强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum
astjson 是一个性能极好的用 Java 语言实现的 JSON 解析器和生成器,来自 阿里巴巴的工程师开发。

主要特点:

  • 快速FAST (比其它任何基于Java的解析器和生成器更快,包括jackson)

  • 强大(支持普通JDK类包括任意Java Bean Class、Collection、Map、Date或enum)

  • 零依赖(没有依赖其它任何类库除了JDK)


一 、生成Json:
JavaBean、List<JavaBean>、List<String>、List<Map<String,Object>>
[java] view plain copy
  1. StringjsonString=JSON.toJSONString(obj);

二、解析Json:
(1)JavaBean
copy
Classclass=JSON.parseObject(jsonString,Class.class);
(2)List<JavaBean>
copy
List<Class>class=JSON.parseArray((jsonString,153); font-weight:bold; background-color:inherit">class);
(3)List<String>
copy
List<String>listString=JSON.parseArray(jsonString,String.(4)List< Map<String,Object> >
copy
List<Map<String,Object>>listMap=JSON.parseObject(jsonString,newTypeReference<List<Map<String,Object>>>(){});

现有这样的json数据:

[javascript] copy
    {"totalRecords":2615,
copy
"result":{"code":"200","status":"success"},
  • "list":[{"unuAbnId":"0bcd930f-014c-1000-e003-5f160a0d0114",
  • "entNo":"1c2e4ca8-00fa-1000-e000-74590a76bf0f",248)"> "regNO":"442000600169663",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "entName":"x",248)"> "entType":"9910",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "speCause":"3",248)"> "abnTime":"Mar13,201512:00:00AM",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "decOrg":"442020",248)"> "entNameUrl":"<ahref="..",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "auditingFileNo":"15000684990326",248)"> "abnormalID":"fd74013d-014b-1000-e00a-72970a0d0114"},{...},...],108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "pageNo":1,248)"> "pageSize":8,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "url":"main/abnInfoPage",248)"> "selList":[{"unuAbnId":"0bcd930f-014c-1000-e003-5f0f0a0d0114",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "entNo":"16da9629-0131-1000-e005-3effc0a803a8",248)"> "regNO":"442000602187424",248)"> "entNameUrl":"<ahref="..">",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "auditingFileNo":"15000684990319",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "topPageNo":1,248)"> "totalPages":327,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "previousPageNo":0,248)"> "nextPageNo":2,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "bottomPageNo":327
  • }
  • 其中list含有2615条数据,selList含有8条数据,目标是提取selList中entNameUrl的链接(不含a href=)
    外层是JSONObject,里面的list和selList是JSONArrary,再里面是JSONObject。其中的result也是JSONObject
    copy
    JSONObjectjsonObj=JSON.parseObject(rawText);
  • JSONArrayresult=jsonObj.getJSONArray("selList");
  • List<Link>links=JSON.parseArray(result.toJSONString(),Link.class);
  • 其中Link类中要有entNameUrl这个属性,并且setter和getter方法。
    在setter方法中可以进一步进行处理
    copy
    publicvoidsetEntNameUrl(StringentNameUrl){
  • this.entNameUrl=Html.create(entNameUrl).links().get();
  • }
  • 这里使用了自定方法,其功能就是取出字符串中的链接。
    Link类中可以包含abnTime、entName、regNO等属性和对应的getter和setter方法,FastJson能自动映射。
    通过下面的方法也可以处理:
    copy
    JSONObjectjsonObj=newJSONObject(rawText);
  • JSONArrayjsonArray=result.getJSONArray("selList");
  • for(inti=0;i<jsonArray.length;i++){
  • }
  • (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读