• 非标准json字符串转换成json格式字符串


    非标准json字符串转换成json格式字符串

    在工作中,会遇到中间件平台输出的日志不是标准json格式的,在接口测试工作中,如果报文节点字段比较多,手工替换耗时,而且难免会出错。

    所以希望用代码,来实现字符串转换。

    非标准的json报文示例如下:

    String a = "{categoryCode=R90046591,pageSize=50,pageNo=1,vendorCode=709028701}";
    String b = "{distributeSys=YHCP,list=[{shelvesStatus=1,cmmdtyCode=0000000007614145711,model=Z,supplierCode=0000000000,b2BDocItemId=1,type=7,chanCode=YH},{shelvesStatus=1,cmmdtyCode=0000000007614145751,model=Z,supplierCode=0000000000,generalCmmdtyCode=0000000007614145711,b2BDocItemId=5,type=7,chanCode=YH}],b2BDocId=11111111}";
    String c = "{supplierCode=713536971,productList=[122996533051,122953951621]}";
    String d = "{supplierCmCode=48913380045271,productCode=11026400731,productTitle=黑人(DARLIE)超白牙膏90g,grossWeight=0.090,productPicList=[{picUrl=https://image/tycfNU-ngWpu3uBNmEcn1A.jpg_800w_800h,picSort=1},{picUrl=https://image/JJmbVALgcTWbuM6A-kmPoQ.jpg_800w_800h,picSort=2},{picUrl=https://image/EkSruhmI3CbjG5n2tisWnQ.jpg_800w_800h,picSort=3},{picUrl=https://image/53rKCwcYTaQ-H-LL_yQFXw.jpg_800w_800h,picSort=4},{picUrl=https://image/aPa4c1U1EwP3CHFiU2FMrQ.jpg_800w_800h,picSort=5}],detailPicList=[{picUrl=http://uimg/sop/commodity/219601388160106773180000_x.jpg,picSort=1},{picUrl=http://uimg/sop/commodity/201508180123139699_x.jpg,picSort=2},{picUrl=http://uimg/sop/commodity/201508180124014879_x.jpg,picSort=3},{picUrl=http://uimg/sop/commodity/203139007101664225245710_x.jpg,picSort=4}],referencePrice=,sellingPrice=6.10,commissionRate=,salesStatus=,sellingPoints=,paramList=[{paramCode=000158,paramValue=中国广东中山市},{paramCode=007930,paramValue=3},{paramCode=005736,paramValue=01}],inventory=9983,vendorCode=714615751,ip=111.111.111.111,operateAccount=669}";

    java代码如下:

    import org.junit.Test;
    
    public class TransStringToJson {
    
        //日志平台报文非标准json格式字符串转为json字符串
        public String stringToJson(String message) {
            String result = "";
            //替换=为":"
            if (message.contains("=")) {
                message = message.replace("=", "":"");
            }
            //替换{为{"
            if (message.contains("{")) {
                message = message.replace("{", "{"");
            }
            //替换}为"}
            if (message.contains("}")) {
                message = message.replace("}", ""}");
            }
            //替换[为["
            if (message.contains("[")) {
                message = message.replace("[", "["");
            }
            //替换]为"]
            if (message.contains("]")) {
                message = message.replace("]", ""]");
            }
            //替换,为","
            if (message.contains(",")) {
                message = message.replace(",", "","");
            }
            //替换"[为[
            if (message.contains(""[")) {
                message = message.replace(""[", "[");
            }
            //替换]"为]
            if (message.contains("]"")) {
                message = message.replace("]"", "]");
            }
            //替换"{为{
            if (message.contains(""{")) {
                message = message.replace(""{", "{");
            }
            //替换}"为}
            if (message.contains("}"")) {
                message = message.replace("}"", "}");
            }
            result = message;
            return result;
        }
    
        @Test
        public void fun1() {
            String a = "{categoryCode=R90046591,pageSize=50,pageNo=1,vendorCode=709028701}";
            String res = stringToJson(a);
            System.out.println(res);
        }
    
        @Test
        public void fun2() {
            String a = "{distributeSys=YHC,list=[{shelvesStatus=1,cmmdtyCode=0000000007614145711,model=Z,supplierCode=0000000000,b2BDocItemId=1,type=7,chanCode=YH},{shelvesStatus=1,cmmdtyCode=0000000007614145751,model=Z,supplierCode=0000000000,generalCmmdtyCode=0000000007614145711,b2BDocItemId=5,type=7,chanCode=YH}],b2BDocId=11111111}";
            String res = stringToJson(a);
            System.out.println(res);
        }
    
        @Test
        public void fun3() {
            String a = "{supplierCode=713536971,productList=[122996533051,122953951621]}";
            String res = stringToJson(a);
            System.out.println(res);
        }
    
        @Test
        public void fun4() {
            String a = "{supplierCmCode=48913380045271,productCode=11026400731,productTitle=黑人(DARLIE)超白牙膏90g,grossWeight=0.090,productPicList=[{picUrl=https://image/tycfNU-ngWpu3uBNmEcn1A.jpg_800w_800h,picSort=1},{picUrl=https://image/JJmbVALgcTWbuM6A-kmPoQ.jpg_800w_800h,picSort=2},{picUrl=https://image/EkSruhmI3CbjG5n2tisWnQ.jpg_800w_800h,picSort=3},{picUrl=https://image/53rKCwcYTaQ-H-LL_yQFXw.jpg_800w_800h,picSort=4},{picUrl=https://image/aPa4c1U1EwP3CHFiU2FMrQ.jpg_800w_800h,picSort=5}],detailPicList=[{picUrl=http://uimg/sop/commodity/219601388160106773180000_x.jpg,picSort=1},{picUrl=http://uimg/sop/commodity/201508180123139699_x.jpg,picSort=2},{picUrl=http://uimg/sop/commodity/201508180124014879_x.jpg,picSort=3},{picUrl=http://uimg/sop/commodity/203139007101664225245710_x.jpg,picSort=4}],referencePrice=,sellingPrice=6.10,commissionRate=,salesStatus=,sellingPoints=,paramList=[{paramCode=000158,paramValue=中国广东中山市},{paramCode=007930,paramValue=3},{paramCode=005736,paramValue=01}],inventory=9983,vendorCode=714615751,ip=111.111.111.111,operateAccount=669}";
            String res = stringToJson(a);
            System.out.println(res);
        }
    }

    测试结果:

    //String a =  {"categoryCode":"R90046591","pageSize":"50","pageNo":"1","vendorCode":"709028701"}
    //String b =  {"distributeSys":"YHC","list":[{"shelvesStatus":"1","cmmdtyCode":"0000000007614145711","model":"Z","supplierCode":"0000000000","b2BDocItemId":"1","type":"7","chanCode":"YH"},{"shelvesStatus":"1","cmmdtyCode":"0000000007614145751","model":"Z","supplierCode":"0000000000","generalCmmdtyCode":"0000000007614145711","b2BDocItemId":"5","type":"7","chanCode":"YH"}],"b2BDocId":"11111111"}
    //String c =  {"supplierCode":"713536971","productList":["122996533051","122953951621"]}
    //String d =  {"supplierCmCode":"48913380045271","productCode":"11026400731","productTitle":"黑人(DARLIE)超白牙膏90g","grossWeight":"0.090","productPicList":[{"picUrl":"https://image/tycfNU-ngWpu3uBNmEcn1A.jpg_800w_800h","picSort":"1"},{"picUrl":"https://image/JJmbVALgcTWbuM6A-kmPoQ.jpg_800w_800h","picSort":"2"},{"picUrl":"https://image/EkSruhmI3CbjG5n2tisWnQ.jpg_800w_800h","picSort":"3"},{"picUrl":"https://image/53rKCwcYTaQ-H-LL_yQFXw.jpg_800w_800h","picSort":"4"},{"picUrl":"https://image/aPa4c1U1EwP3CHFiU2FMrQ.jpg_800w_800h","picSort":"5"}],"detailPicList":[{"picUrl":"http://uimg/sop/commodity/219601388160106773180000_x.jpg","picSort":"1"},{"picUrl":"http://uimg/sop/commodity/201508180123139699_x.jpg","picSort":"2"},{"picUrl":"http://uimg/sop/commodity/201508180124014879_x.jpg","picSort":"3"},{"picUrl":"http://uimg/sop/commodity/203139007101664225245710_x.jpg","picSort":"4"}],"referencePrice":"","sellingPrice":"6.10","commissionRate":"","salesStatus":"","sellingPoints":"","paramList":[{"paramCode":"000158","paramValue":"中国广东中山市"},{"paramCode":"007930","paramValue":"3"},{"paramCode":"005736","paramValue":"01"}],"inventory":"9983","vendorCode":"714615751","ip":"111.111.111.111","operateAccount":"669"}

    使用bejson网站(http://www.bejson.com/)工具将测试结果格式化校验,便于查看节点明细

    /*String a = 
        {
            "categoryCode": "R90046591",
                "pageSize": "50",
                "pageNo": "1",
                "vendorCode": "709028701"
        }*/
    /*String b=
            {
            "distributeSys":"YHC",
            "list":[{
            "shelvesStatus":"1",
            "cmmdtyCode":"0000000007614145711",
            "model":"Z",
            "supplierCode":"0000000000",
            "b2BDocItemId":"1",
            "type":"7",
            "chanCode":"YH"
            },{
            "shelvesStatus":"1",
            "cmmdtyCode":"0000000007614145751",
            "model":"Z",
            "supplierCode":"0000000000",
            "generalCmmdtyCode":"0000000007614145711",
            "b2BDocItemId":"5",
            "type":"7",
            "chanCode":"YH"
            }],
            "b2BDocId":"11111111"
            }*/
    /*String c=
            {
            "supplierCode":"713536971",
            "productList":["122996533051","122953951621"]
            }*/
    /*String d=
            {
            "supplierCmCode":"48913380045271",
            "productCode":"11026400731",
            "productTitle":"黑人(DARLIE)超白牙膏90g",
            "grossWeight":"0.090",
            "productPicList":[{
            "picUrl":"https://image/tycfNU-ngWpu3uBNmEcn1A.jpg_800w_800h",
            "picSort":"1"
            },{
            "picUrl":"https://image/JJmbVALgcTWbuM6A-kmPoQ.jpg_800w_800h",
            "picSort":"2"
            },{
            "picUrl":"https://image/EkSruhmI3CbjG5n2tisWnQ.jpg_800w_800h",
            "picSort":"3"
            },{
            "picUrl":"https://image/53rKCwcYTaQ-H-LL_yQFXw.jpg_800w_800h",
            "picSort":"4"
            },{
            "picUrl":"https://image/aPa4c1U1EwP3CHFiU2FMrQ.jpg_800w_800h",
            "picSort":"5"
            }],
            "detailPicList":[{
            "picUrl":"http://uimg/sop/commodity/219601388160106773180000_x.jpg",
            "picSort":"1"
            },{
            "picUrl":"http://uimg/sop/commodity/201508180123139699_x.jpg",
            "picSort":"2"
            },{
            "picUrl":"http://uimg/sop/commodity/201508180124014879_x.jpg",
            "picSort":"3"
            },{
            "picUrl":"http://uimg/sop/commodity/203139007101664225245710_x.jpg",
            "picSort":"4"
            }],
            "referencePrice":"",
            "sellingPrice":"6.10",
            "commissionRate":"",
            "salesStatus":"",
            "sellingPoints":"",
            "paramList":[{
            "paramCode":"000158",
            "paramValue":"中国广东中山市"
            },{
            "paramCode":"007930",
            "paramValue":"3"
            },{
            "paramCode":"005736",
            "paramValue":"01"
            }],
            "inventory":"9983",
            "vendorCode":"714615751",
            "ip":"111.111.111.111",
            "operateAccount":"669"
            }*/

     

  • 相关阅读:
    Django URL 命名空间
    数据库
    iOS之网络数据下载和JSON解析
    IOS之AFNetworking的使用
    IOS之网络数据下载和JSON解析
    IOS之XML解析
    iOS网络基础知识
    iOS开发--Block
    IOS开发——使用数据库
    IOS 开发下拉刷新和上拉加载更多
  • 原文地址:https://www.cnblogs.com/KevinFeng/p/15018798.html
Copyright © 2020-2023  润新知