zoukankan      html  css  js  c++  java
  • fastjson与net.sf.json区别

      在现在的开发当中,绝大多数引用阿里巴巴的fastjson。当然net.sf.json同样可以使用。

    一、引入net.sf.json包

      首先用net.sf.json包,当然你要导入很多包来支持commons-beanutils-1.7.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging.jar ezmorph-1.0.3.jar json-lib-2.1-jdk15.jar,自己百度下载吧

    然后使用方法:

    json串:{"code":0,"msg":"成功","data":{"template":{"templateId":23,"code":"redmine","type":0,"status":0,"nodeName":"会员期限即将到期","detail":"1个月","content":"您的会员..."}}}

    调用:

    Map<String,String> dto = new HashMap<String,String>();
    dto.put("code", app.getCode());

    xxService.getxx(JSONObject.fromObject(dto).toString());

    解析:

    JSONObject jsonObject = JSONObject.fromObject(msgContent);  
    JSONObject o2=JSONObject.fromObject(jsonObject.get("data")); 
    JSONObject o3=JSONObject.fromObject(o2.get("template"));

    (String)o3.get("content")

    如果是json串中使用了[]的这种就需要用到JSONArray

    二、引入com.alibaba.fastjson包

    调用:

    Map<String,String> dto = new HashMap<String,String>();
            dto.put("code", "haha");
            System.out.println(JSONObject.toJSON(dto).toString());

    解析:

    JSONObject jsonObject = JSONObject.parseObject(result);         
    JSONObject o2=JSONObject.parseObject((String) jsonObject.getString("data")); 
    JSONObject o3=JSONObject.parseObject((String) o2.getString("template")); 
    System.out.println((String) jsonObject.getString("data"));
    System.out.println((String) o2.getString("template"));
    System.out.println((String) o3.getString("content"));

  • 相关阅读:
    最全QQ空间说说伪装代码
    Office文件找回技巧
    CentOS7安装CMake(arm版)华为云服务器
    centos7修改ssh端口
    CentOS7安装zookeeper(ARM)版——华为服务器
    CentOS7安装JDK1.8
    Centos7安装Docker
    Prometheus+mysqld_exporter
    Prometheus+blackbox_exporter
    Prometheus+node_exporter
  • 原文地址:https://www.cnblogs.com/wangzhengyu/p/9907917.html
Copyright © 2011-2022 走看看