zoukankan      html  css  js  c++  java
  • fastjson 使用记录

    参考:

    https://www.cnblogs.com/cdf-opensource-007/p/7106018.html

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    public class JsonTest2 {
    
        public static void main(String[] args) {
    
            String arrContent = "[
    " +
                    "    {
    " +
                    "      "astnamebyuser": "资产名称1",
    " +
                    "      "ip": 10,
    " +
                    "      "port": "",
    " +
                    "      "companytypeid": "",//组织机构一级导航id
    " +
                    "      "deptid": "",//组织架构二级导航id
    " +
                    "      "infosysid": "",//信息系统id
    " +
                    "      "devicetypeid": "",//设备类型id
    " +
                    "      "templategroupid":"",//模板分组id
    " +
                    "      "templateid": "",//模板id
    " +
                    "      "templatename": "",//如果非自定义模板传null,如果是自定义模板传自定义名称
    " +
                    "      "certificateusername": "",//用户名
    " +
                    "      "certificatepassword": "",//密码
    " +
                    "      "principal": "",//维护人
    " +
                    "      "department": "",//所属部门
    " +
                    "      "description": ""//备注
    " +
                    "    },
    " +
                    "    {
    " +
                    "      "astnamebyuser": "资产名称2",
    " +
                    "      "ip": 11,
    " +
                    "      "port": "",
    " +
                    "      "companytypeid": "",//组织机构一级导航id
    " +
                    "      "deptid": "",//组织架构二级导航id
    " +
                    "      "infosysid": "",//信息系统id
    " +
                    "      "devicetypeid": "",//设备类型id
    " +
                    "      "templategroupid":"",//模板分组id
    " +
                    "      "templateid": "",//模板id
    " +
                    "      "templatename": "",//如果非自定义模板传null,如果是自定义模板传自定义名称
    " +
                    "      "certificateusername": "",//用户名
    " +
                    "      "certificatepassword": "",//密码
    " +
                    "      "principal": "",//维护人
    " +
                    "      "department": "",//所属部门
    " +
                    "      "description": ""//备注
    " +
                    "    }
    " +
                    "  ]";
    
    
            JSONArray jsonArray = JSON.parseArray(arrContent);
            for (Object obj :
                    jsonArray) {
                JSONObject jsonObject = (JSONObject) obj;
    //            这里要看传的是String还是Integer,得按照对应的数据类型来获取,否则会抛出异常
                System.out.println(jsonObject.getString("astnamebyuser")+" "+jsonObject.getInteger("ip"));
            }
    
    
        }
    
    }

    目前所在公司的前端一般一律传字符串,所以用起来还是挺方便的,直接 getString 就行了,不过如果传的是特定的数据类型,就得用特定的方法来获取,比如 integer类型的,就要用 getInteger 来进行获取

    下面这个是把循环遍历中的obj通过getString/getInteger/其他获取数据类型的方法  来获取键的值,然后用bean的set方法进行赋值,最后对整个bean进行处理就行了

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    import org.kinome.rm.bean.Assets;
    
    public class JsonTest2 {
    
        public static void main(String[] args) {
    
            String content = "{
    " +
                    "  "ipgrouplist":[
    " +
                    "    {
    " +
                    "      "astnamebyuser": "资产名称1",
    " +
                    "      "ip": "192.168.1.1",
    " +
                    "      "port": "",
    " +
                    "      "companytypeid": "",//组织机构一级导航id
    " +
                    "      "deptid": "",//组织架构二级导航id
    " +
                    "      "infosysid": "",//信息系统id
    " +
                    "      "devicetypeid": "",//设备类型id
    " +
                    "      "templategroupid":"",//模板分组id
    " +
                    "      "templateid": "",//模板id
    " +
                    "      "templatename": "",//如果非自定义模板传null,如果是自定义模板传自定义名称
    " +
                    "      "certificateusername": "",//用户名
    " +
                    "      "certificatepassword": "",//密码
    " +
                    "      "principal": "",//维护人
    " +
                    "      "department": "",//所属部门
    " +
                    "      "description": ""//备注
    " +
                    "    },
    " +
                    "    {
    " +
                    "      "astnamebyuser": "资产名称2",
    " +
                    "      "ip": "192.168.1.2",
    " +
                    "      "port": "",
    " +
                    "      "companytypeid": "",//组织机构一级导航id
    " +
                    "      "deptid": "",//组织架构二级导航id
    " +
                    "      "infosysid": "",//信息系统id
    " +
                    "      "devicetypeid": "",//设备类型id
    " +
                    "      "templategroupid":"",//模板分组id
    " +
                    "      "templateid": "",//模板id
    " +
                    "      "templatename": "",//如果非自定义模板传null,如果是自定义模板传自定义名称
    " +
                    "      "certificateusername": "",//用户名
    " +
                    "      "certificatepassword": "",//密码
    " +
                    "      "principal": "",//维护人
    " +
                    "      "department": "",//所属部门
    " +
                    "      "description": ""//备注
    " +
                    "    }
    " +
                    "  ],
    " +
                    "  "ipgroupname":"ip组1",
    " +
                    "  "execmethod":"1",
    " +
                    "  "taskdescription":"描述"
    " +
                    "
    " +
                    "}";
    
    
            JSONObject parseObject = JSON.parseObject(content);
            JSONArray jsonArray = parseObject.getJSONArray("ipgrouplist");
            String ipgroupname = parseObject.getString("ipgroupname");
            String execmethod = parseObject.getString("execmethod");
            String taskdescription = parseObject.getString("taskdescription");
            Assets assets = new Assets();
    
            System.out.println(ipgroupname + " " + execmethod + " " + taskdescription);
            for (Object obj :
                    jsonArray) {
                JSONObject jsonObject = (JSONObject) obj;
                assets.setIp(jsonObject.getString("ip"));
                System.out.println(jsonObject.toString());
            }
    
    
        }
    
    }
  • 相关阅读:
    RAW和JPEG的区别_ZT
    用户自定义基元UDP_ZT
    UDP用户自定义原语
    SR锁存器
    Matlab实现Butterworth滤波器 分类: 图像处理 2014-06-02 00:05 527人阅读 评论(0) 收藏
    egrep命令的实现 分类: 编译原理 2014-06-01 23:41 329人阅读 评论(0) 收藏
    随机L系统分形树 分类: 计算机图形学 2014-06-01 23:27 376人阅读 评论(0) 收藏
    matlab实现算术编解码 分类: 图像处理 2014-06-01 23:01 357人阅读 评论(0) 收藏
    命名管道实现进程间通信--石头、剪刀、布游戏 分类: linux 2014-06-01 22:50 467人阅读 评论(0) 收藏
    互斥锁与条件变量应用 2014-06-01 22:20 328人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/kinome/p/10302503.html
Copyright © 2011-2022 走看看