java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > java String拼接json

java String拼接json的方式实现

作者:进击中的程序媛

本文主要介绍了java String拼接json的方式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

首先要晓得json显示的格式,就晓得下面为啥要这样拼接了

1.通过转义符号拼接

String content2 ="{\"order\":{\"terminalNumber\":\""+""+"\""
                + ",\"listFlag\":"+"\""+"0"+"\""
                + ", \"pushMode\":"+"\""+"1"+"\""
                + ",\"managerCardNo\":"+"\""+""+"\""
                + ",\"departmentId\":"+"\""+"9F7E9439CA8B4C60A2FFF3EA3290B088"+"\""
                + ",\"paperInvoiceType\":"+"\""+""+"\""
                + ",\"checker\":"+"\""+"王五"+"\""
                + ",\"invoiceNumEnd\":"+"\""+""+"\""
                + ",\"payee\":"+"\""+"李四"+"\""
                + ",\"buyerAddress\":"+"\""+"杭州市"+"\""
                + ",\"invoiceType\":"+"\""+"1"+"\""
                + ",\"specificFactor\":"+"\""+"0"+"\""
                + ",\"orderNo\":"+"\""+"11111332244"+"\""
                + ",\"machineCode\":"+"\""+""+"\""
                + ",\"vehicleFlag\":"+"\""+"1"+"\""
                + ",\"invoiceCode\":"+"\""+""+"\""
                + ",\"buyerPhone\":"+"\""+"15858585858"+"\""
                + ",\"surveyAnswerType\":"+"\""+""+"\""
                + ",\"invoiceDetail\":[{\"specType\":"+"\""+"y460"+"\""
                + ",\"taxExcludedAmount\":"+"\""+"0.88"+"\""
                + ",\"invoiceLineProperty\":"+"\""+"0"+"\""
                + ",\"favouredPolicyName\":"+"\""+"0"+"\""
                + ",\"num\":"+"\""+""+"\""
                + ",\"withTaxFlag\":"+"\""+"1"+"\""
                + ",\"tax\":"+"\""+"0.12"+"\""
                + ",\"favouredPolicyFlag\":"+"\""+"0"+"\""
                + ",\"taxRate\":"+"\""+"0.13"+"\""
                + ",\"unit\":"+"\""+"台"+"\""
                + ",\"deduction\":"+"\""+"0"+"\""
                + ",\"price\":"+"\""+""+"\""
                + ",\"zeroRateFlag\":"+"\""+"0"+"\""
                + ",\"goodsCode\":"+"\""+"1090511030000000000"+"\""
                + ",\"selfCode\":"+"\""+"130005426000000000"+"\""
                + ",\"goodsName\":"+"\""+"电脑"+"\""
                + ",\"taxIncludedAmount\":"+"\""+"1"+ "\"}]"
                + ",\"buyerTel\":"+"\""+"0571-88888888"+"\""
                + ",\"nextInvoiceNum\":"+"\""+""+"\""
                + ",\"clerkId\":"+"\""+""+"\""
                + ",\"remark\":"+"\""+"备注信息"+"\""
                + ",\"managerCardType\":"+"\""+"201"+"\""
                + ",\"buyerTaxNum\":"+"\""+"339901999999198"+"\""
                + ",\"invoiceLine\":"+"\""+"p"+"\""
                + ",\"secondHandCarInfo\": {\"organizeType\":"+"\""+"1"+"\""
                + ",\"vehicleManagementName\":"+"\""+"杭州"+"\""
                + ",\"sellerPhone\":"+"\""+"13888888888"+"\""
                + ",\"sellerName\":"+"\""+"张三"+"\""
                + ",\"brandModel\":"+"\""+"宝马3系"+"\""
                + ",\"vehicleCode\":"+"\""+"LHGK43284342384234"+"\""
                + ",\"licenseNumber\":"+"\""+"浙A12345"+"\""
                + ",\"registerCertNo\":"+"\""+"330022123321"+"\""
                + ",\"sellerAddress\":"+"\""+"杭州文一路888号"+"\""
                + ",\"vehicleType\":"+"\""+"轿车"+"\""
                + ",\"intactCerNum\":"+"\""+""+"\""
                + ",\"sellerTaxnum\":"+"\""+"330100199001010000"+"\"}"
                + ",\"buyerManagerName\":"+"\""+"张三"+"\""
                + ",\"redReason\":"+"\""+"1"+"\""
                + ",\"email\":"+"\""+"test@xx.com"+"\""
                + ",\"salerAccount\":"+"\""+""+"\""
                + ",\"salerTel\":"+"\""+"0571-77777777"+"\""
                + ",\"callBackUrl\":"+"\""+"http:127.0.0.1/invoice/callback/"+"\""
                /*
                 * + ",\"additionalElementList\": [{\"elementValue\":"+"\""+"信息值"+"\"" +
                 * ",\"elementType\":"+"\""+"信息类型"+"\"" + ",\"elementName\":"+"\""+"信息名称"+"\"}]"
                 */
                + ",\"billInfoNo\":"+"\""+"1403011904008472"+"\""
                /*
                 * + ",\"vehicleInfo\": {\"taxOfficeCode\":"+"\""+"13399000"+"\"" +
                 * ",\"manufacturerName\":"+"\""+"华晨宝马汽车生产有限公司"+"\"" +
                 * ",\"importCerNum\":"+"\""+""+"\"" +
                 * ",\"certificate\":"+"\""+"WDL042613263551"+"\"" +
                 * ",\"engineNum\":"+"\""+"10111011111"+"\"" +
                 * ",\"taxOfficeName\":"+"\""+"杭州税务"+"\"" + ",\"brandModel\":"+"\""+"宝马3系"+"\""
                 * + ",\"productOrigin\":"+"\""+"北京"+"\"" +
                 * ",\"vehicleCode\":"+"\""+"LHGK43284342384234"+"\"" +
                 * ",\"maxCapacity\":"+"\""+"5"+"\"" + ",\"intactCerNum\":"+"\""+""+"\"" +
                 * ",\"tonnage\":"+"\""+"2"+"\"" + ",\"insOddNum\":"+"\""+""+"\"" +
                 * ",\"idNumOrgCode\":"+"\""+"9114010034683511XD"+"\"" +
                 * ",\"vehicleType\":"+"\""+"轿车"+ "\"}"
                 */
                + ",\"buyerName\":"+"\""+"企业名称/个人"+"\""
                + ",\"invoiceDate\":"+"\""+"2022-01-13 12:30:00"+"\""
                + ",\"invoiceNum\":"+"\""+""+"\""
                + ",\"hiddenBmbbbh\":"+"\""+"0"+"\""
                + ",\"salerAddress\":"+"\""+""+"\""
                + ",\"clerk\":"+"\""+"张三"+"\""
                + ",\"buyerAccount\":"+"\""+"中国工商银行 111111111111"+"\""
                + ",\"nextInvoiceCode\":"+"\""+""+"\""
                + ",\"extensionNumber\":"+"\""+extensionNumber+"\""
                + ",\"additionalElementName\":"+"\""+"测试模版"+"\""
                + ",\"salerTaxNum\":"+"\""+"339901999999199"+"\""
                + ",\"listName\":"+"\""+"详见销货清单"+"\""
                + ",\"proxyInvoiceFlag\":"+"\""+"0"+"\"}}";

利用postMan不会报错

2.通过map的方式构造json

Map inputMap = new HashMap();
        Map orderMap = new HashMap();
        orderMap.put("buyerName", requestInfoVO.getInfoClientName());
        orderMap.put("buyerTaxNum", requestInfoVO.getInfoClientTaxCode());
        List<Map> invDataList = new ArrayList<Map>();
        for (int i = 0, len = requestInfoVO.getGoodsList().size(); i < len; i++) {
            InvoiceRequestListItemVO itemVO = requestInfoVO.getGoodsList().get(i);
            Map invoiceDetailMap = new HashMap();
            invoiceDetailMap.put("goodsName", itemVO.getGoodsName());
            invoiceDetailMap.put("goodsCode", "");
            invDataList.add(invoiceDetailMap);
        }
        orderMap.put("invoiceDetail", invDataList);
        inputMap.put("order", orderMap);
        String jsonStr = JSON.toJSONString(inputMap);         

也可以实现以上的效果 亲测有效!!

附带简洁版:原理一样

String inputJson = "";
Map inputMap = new HashMap();
// user
Map userMap = new HashMap();
userMap.put("loginName", "));
userMap.put("loginPassword","));
inputMap.put("userInfo", userMap);
// data
Map bussdataMap = new HashMap();
bussdataMap.put("tableName", "");
List<Map> dataList = new ArrayList<Map>();
Map dataMap = new HashMap();
// 
dataMap.put("id", "");
dataMap.put("id_card_no","" );
dataList.add(dataMap);
bussdataMap.put("row", dataList);
inputMap.put("dataInfo", bussdataMap);
// operationType
inputMap.put("operate", "插入");
JSONWriter writer = new JSONWriter(false);
inputJson = writer.write(inputMap);

到此这篇关于java String拼接json的方式实现的文章就介绍到这了,更多相关java String拼接json内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文