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 = "[\n" +
                "    {\n" +
                "      \"astnamebyuser\": \"资产名称1\",\n" +
                "      \"ip\": 10,\n" +
                "      \"port\": \"\",\n" +
                "      \"companytypeid\": \"\",//组织机构一级导航id\n" +
                "      \"deptid\": \"\",//组织架构二级导航id\n" +
                "      \"infosysid\": \"\",//信息系统id\n" +
                "      \"devicetypeid\": \"\",//设备类型id\n" +
                "      \"templategroupid\":\"\",//模板分组id\n" +
                "      \"templateid\": \"\",//模板id\n" +
                "      \"templatename\": \"\",//如果非自定义模板传null,如果是自定义模板传自定义名称\n" +
                "      \"certificateusername\": \"\",//用户名\n" +
                "      \"certificatepassword\": \"\",//密码\n" +
                "      \"principal\": \"\",//维护人\n" +
                "      \"department\": \"\",//所属部门\n" +
                "      \"description\": \"\"//备注\n" +
                "    },\n" +
                "    {\n" +
                "      \"astnamebyuser\": \"资产名称2\",\n" +
                "      \"ip\": 11,\n" +
                "      \"port\": \"\",\n" +
                "      \"companytypeid\": \"\",//组织机构一级导航id\n" +
                "      \"deptid\": \"\",//组织架构二级导航id\n" +
                "      \"infosysid\": \"\",//信息系统id\n" +
                "      \"devicetypeid\": \"\",//设备类型id\n" +
                "      \"templategroupid\":\"\",//模板分组id\n" +
                "      \"templateid\": \"\",//模板id\n" +
                "      \"templatename\": \"\",//如果非自定义模板传null,如果是自定义模板传自定义名称\n" +
                "      \"certificateusername\": \"\",//用户名\n" +
                "      \"certificatepassword\": \"\",//密码\n" +
                "      \"principal\": \"\",//维护人\n" +
                "      \"department\": \"\",//所属部门\n" +
                "      \"description\": \"\"//备注\n" +
                "    }\n" +
                "  ]";


        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 = "{\n" +
                "  \"ipgrouplist\":[\n" +
                "    {\n" +
                "      \"astnamebyuser\": \"资产名称1\",\n" +
                "      \"ip\": \"192.168.1.1\",\n" +
                "      \"port\": \"\",\n" +
                "      \"companytypeid\": \"\",//组织机构一级导航id\n" +
                "      \"deptid\": \"\",//组织架构二级导航id\n" +
                "      \"infosysid\": \"\",//信息系统id\n" +
                "      \"devicetypeid\": \"\",//设备类型id\n" +
                "      \"templategroupid\":\"\",//模板分组id\n" +
                "      \"templateid\": \"\",//模板id\n" +
                "      \"templatename\": \"\",//如果非自定义模板传null,如果是自定义模板传自定义名称\n" +
                "      \"certificateusername\": \"\",//用户名\n" +
                "      \"certificatepassword\": \"\",//密码\n" +
                "      \"principal\": \"\",//维护人\n" +
                "      \"department\": \"\",//所属部门\n" +
                "      \"description\": \"\"//备注\n" +
                "    },\n" +
                "    {\n" +
                "      \"astnamebyuser\": \"资产名称2\",\n" +
                "      \"ip\": \"192.168.1.2\",\n" +
                "      \"port\": \"\",\n" +
                "      \"companytypeid\": \"\",//组织机构一级导航id\n" +
                "      \"deptid\": \"\",//组织架构二级导航id\n" +
                "      \"infosysid\": \"\",//信息系统id\n" +
                "      \"devicetypeid\": \"\",//设备类型id\n" +
                "      \"templategroupid\":\"\",//模板分组id\n" +
                "      \"templateid\": \"\",//模板id\n" +
                "      \"templatename\": \"\",//如果非自定义模板传null,如果是自定义模板传自定义名称\n" +
                "      \"certificateusername\": \"\",//用户名\n" +
                "      \"certificatepassword\": \"\",//密码\n" +
                "      \"principal\": \"\",//维护人\n" +
                "      \"department\": \"\",//所属部门\n" +
                "      \"description\": \"\"//备注\n" +
                "    }\n" +
                "  ],\n" +
                "  \"ipgroupname\":\"ip组1\",\n" +
                "  \"execmethod\":\"1\",\n" +
                "  \"taskdescription\":\"描述\"\n" +
                "\n" +
                "}";


        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());
        }


    }

}

 

posted @ 2019-01-22 10:44  由良大小姐  阅读(198)  评论(0编辑  收藏  举报