url传递json串参数及后台处理【我】

用url请求向后台传参时,如果参数是一个对象序列成的json传,直接传递会报错,可以用 encodeURIComponent 进行转码【将json中的{}等特殊符号转码】后再传递,java后台基本不用特殊处理。

js页面跳转:

window.location.href = ctxPath + '/order/custOperate!toAddCust.action?custJson=' + encodeURIComponent(JSON2.stringify(custObj))

java后台处理解析:

package com.test;

import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

public class Json1 {

    public static void main(String[] args) {

//        String reqJson = _request.getParameter("reqJson");
        String reqJson = "{"idExpDate":"2099-12-31","partyName":"华夏银行股份有限公司","partyNumber":"BJ2018122110041065","identityType":"2","custTree":{"custTreeTopNodeCode":"100207","controlArea":"","controlAreaTop":"","custTreeTopName":"华夏银行股份有限公司","custTreeNodeCode":"100207","controlDepTop":"13007005","controlTypeTop":"13013001","controlType":"13013001","custTreeName":"华夏银行股份有限公司","controlDep":"13007005"},"orgUscc":"9111000010112001XW","partyCert":[{"certNum":"qazwsxed3","certType":"","partyCertId":"259941363"},{"certNum":"9111000010112001XW","certType":"49","partyCertId":"820108430"}],"industryCd":"DD0302","isAvailable":"1","idEffDate":"1992-10-14","industryTypeId":"177","orgStatusCd":"4","partyId":"259941363","registerAddr":"北京市东城区建国门内大街22号"}";
        JSONObject jsonData = JSONObject.parseObject(reqJson);
        String partyName = String.valueOf(jsonData.get("partyName"));
        List<Map<String, String>> partyCertList = (List<Map<String, String>>) jsonData.get("partyCert");
        for (int i = 0; i < partyCertList.size(); i++) {
            Map partyCertMap = partyCertList.get(i);
            System.out.println(partyCertMap);
        }
    }
}

/*

{
    "idExpDate": "2099-12-31",
    "partyName": "华夏银行股份有限公司",
    "partyNumber": "BJ2018122110041065",
    "identityType": "2",
    "custTree": {
        "custTreeTopNodeCode": "100207",
        "controlArea": "",
        "controlAreaTop": "",
        "custTreeTopName": "华夏银行股份有限公司",
        "custTreeNodeCode": "100207",
        "controlDepTop": "13007005",
        "controlTypeTop": "13013001",
        "controlType": "13013001",
        "custTreeName": "华夏银行股份有限公司",
        "controlDep": "13007005"
    },
    "orgUscc": "9111000010112001XW",
    "partyCert": [
        {
            "certNum": "qazwsxed3",
            "certType": "",
            "partyCertId": "259941363"
        },
        {
            "certNum": "9111000010112001XW",
            "certType": "49",
            "partyCertId": "820108430"
        }
    ],
    "industryCd": "DD0302",
    "isAvailable": "1",
    "idEffDate": "1992-10-14",
    "industryTypeId": "177",
    "orgStatusCd": "4",
    "partyId": "259941363",
    "registerAddr": "北京市东城区建国门内大街22号"
}


*/
原文地址:https://www.cnblogs.com/libin6505/p/12144338.html