• 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());
            }
    
    
        }
    
    }
  • 相关阅读:
    网络编程
    反射函数与元类
    面向对象进阶
    对象的封装与接口
    对象继承
    面向对象
    包,logging模块与haslib模块
    闭包函数及装饰器
    函数对象、函数的嵌套、名称空间及作用域
    函数简介及函数参数介绍
  • 原文地址:https://www.cnblogs.com/kinome/p/10302503.html
Copyright © 2020-2023  润新知