当前位置: 首页 > 工具软件 > json4g > 使用案例 >

String转化为Json,并取转化为Json格式后的值

东郭腾
2023-12-01

{
    "dispatchId":"DDTwaterflowsFlow001",
    "agencyId":"superfriday",
    "sessionNbr":"a00001",
    "deviceNumber":"17611191179",
    "areaCode":"",
    "birthday":"19910302",
    "channelCode":"100011",
    "cityCode":"110",
    "isLogin":"1",
    "location":"116.365201,39.916456",
    "provinceCode":"011",
    "serialNumber":"18538389250",
    "traceId":"10000120200813173848131539456590",
    "currentTouch":"10011",
    "additionalInfoJson":{
        "brand":"4G00",
        "cert_type":"02",
        "custID":"7516051594206509",
        "cust_sex":"1",
        "group_info":[
            {
                "group_id":"9818050424868369",
                "group_type":"00",
                "main_card_flag":"",
                "main_num_flag":"1",
                "product_id":"90308943"
            }
        ],
        "is_inuser":"0000",
        "last_stat_date":"20210330151028",
        "net_type":"11",
        "open_date":"20160430140210",
        "package_id":"90063345",
        "pay_type":"2",
        "productTypeN":"01",
        "roam_stat":"4",
        "subscrb_type":"0",
        "user_status":"0"
    },
    "reqFlag":1
}

public static void main(String[] args) {
        String serviceName="{\"dispatchId\":\"DDTwaterflowsFlow001\",\"agencyId\":\"superfriday\",\"sessionNbr\":\"a00001\",\"deviceNumber\":\"17611191179\",\"areaCode\":\"\",\"birthday\":\"19910302\",\"channelCode\":\"100011\",\"cityCode\":\"110\",\"isLogin\":\"1\",\"location\":\"116.365201,39.916456\",\"provinceCode\":\"011\",\"serialNumber\":\"18538389250\",\"traceId\":\"10000120200813173848131539456590\",\"currentTouch\":\"10011\",\"additionalInfoJson\":{\"brand\":\"4G00\",\"cert_type\":\"02\",\"custID\":\"7516051594206509\",\"cust_sex\":\"1\",\"group_info\":[{\"group_id\":\"9818050424868369\",\"group_type\":\"00\",\"main_card_flag\":\"\",\"main_num_flag\":\"1\",\"product_id\":\"90308943\"}],\"is_inuser\":\"0000\",\"last_stat_date\":\"20210330151028\",\"net_type\":\"11\",\"open_date\":\"20160430140210\",\"package_id\":\"90063345\",\"pay_type\":\"2\",\"productTypeN\":\"01\",\"roam_stat\":\"4\",\"subscrb_type\":\"0\",\"user_status\":\"0\"}, \"channelCode\":\"100011\",\"reqFlag\":1}";
        System.out.println("转化为Json格式后得serviceName对象:"+serviceName);
        //转换为JSON格式
        JSONObject jsonObject = JSONObject.parseObject(serviceName);
        //获取jsonObject下的属性
        Object dispatchId = jsonObject.get("dispatchId");
        System.out.println("serviceName对象下的dispatchId属性:"+dispatchId);
        //获取jsonObject下的additionalInfoJson对象
        JSONObject additionalInfoJson = jsonObject.getJSONObject("additionalInfoJson");
        System.out.println("serviceName对象下的additionalInfoJson对象:"+additionalInfoJson);
        //获取jsonObject下的additionalInfoJson对象下的属性
        Object brand = additionalInfoJson.get("brand");
        System.out.println("additionalInfoJson对象下的brand属性:"+brand);
        //获取jsonObject下的additionalInfoJson对象下的数组
        JSONArray group_info = additionalInfoJson.getJSONArray("group_info");
        for (int i = 0; i < group_info.size(); i++) {
            JSONObject obj = group_info.getJSONObject(i);
            Object group_type = obj.get("group_type");
            System.out.println("additionalInfoJson对象下的group_info数组下的group_type属性:"+group_type);
        }
    }

 类似资料: