优秀的编程知识分享平台

网站首页 > 技术文章 正文

Java将字符串解析为Json格式

nanyue 2025-01-13 16:34:56 技术文章 1 ℃

解析json字符串格式如下

"[[{"Result":{"ResponseStatus":{"ErrorCode":500,"IsSuccess":false,"Errors":[{"FieldName":null,"Message":"名称为“生产管理”的模块/子系统未购买","DIndex":0}],"SuccessEntitys":[],"SuccessMessages":[],"MsgCode":7}}}]]";

 
String cookie = login();
          String result = ExecuteBillQuery(cookie);
          System.out.println(result);
        String[][] parse11 = JSON.parseObject(result, String[][].class);
        List<List<String>> lists11 = JSON.parseObject(JSON.toJSONString(parse11),new TypeReference<List<List<String>>>() {});
        System.out.println("lists11");
        lists11.forEach(i-> {
            System.out.println(i);
            System.out.println(i.get(0));
            String result0 = i.get(0);
            JSONObject jsonObject =  JSON.parseObject(result0);
            System.out.println("jsonObject");
            System.out.println(jsonObject);

            JSONObject result2 = (JSONObject) jsonObject.get("Result");
            JSONObject result3 = (JSONObject) result2.get("ResponseStatus");
            System.out.println("result3");
            System.out.println(result3);

            //遍历key和value
            for (Map.Entry<String, Object> entry : result3.entrySet()) {
                System.out.println("这条JSON的Key是:"+entry.getKey()+"这条JSON的Value是:"+ entry.getValue());
                if ("Errors".equals(entry.getKey()))
                {
                    JSONArray error = (JSONArray) entry.getValue();
                    JSONObject message = JSON.parseObject(error.getString(0));
                    System.out.println(message.get("Message"));
                }
            }



        });

解析字符串数组如下


String str2="[[\"MO10003000080\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-21T00:00:00\",\"C\",\"1\",115084,\"WR705(规格:JDF)\",0,100080,10095,753.0000000000,\"7\",0,\"1\"],[\"MO10003000012\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",115098,\"WR7168(规格:F2)\",0,100080,10095,125.0000000000,\"7\",0,\"1\"],[\"MO10003000080\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-21T00:00:00\",\"C\",\"1\",115169,\"WR7501PB(规格:7)\",0,100080,10095,6.5000000000,\"7\",0,\"1\"],[\"MO10003000013\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",115203,\"WR7501PB(规格:1Y )\",0,100080,10095,10.0000000000,\"7\",0,\"1\"],[\"MO10003000029\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-16T00:00:00\",\"C\",\"1\",115205,\"WR7399 A(规格:D3)\",0,100080,10095,82.0000000000,\"7\",0,\"1\"],[\"MO10003000153\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-23T00:00:00\",\"C\",\"1\",115205,\"WR7399 A(规格:D3)\",0,100080,10095,193.0000000000,\"7\",0,\"1\"],[\"MO10003000031\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-16T00:00:00\",\"C\",\"1\",115226,\"WR7399B(规格:D5L)\",0,100080,10095,99.0000000000,\"7\",0,\"1\"],[\"MO10003000383\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-30T00:00:00\",\"C\",\"1\",115380,\"WR7501(规格:T7)\",0,100080,10095,20.8800000000,\"7\",223760,\"1\"],[\"MO10001000076\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-26T00:00:00\",\"C\",\"1\",122393,\"WH-3100-J\",0,100078,10095,369.0000000000,\"7\",221598,\"1\"],[\"MO10001000076\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-26T00:00:00\",\"C\",\"1\",122438,\"WH-1100-J-126\",0,100078,10095,420.0000000000,\"7\",221190,\"1\"],[\"MO10001000076\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-26T00:00:00\",\"C\",\"1\",122450,\"WH-3100-JZH\",0,100078,10095,316.0000000000,\"7\",221199,\"1\"],[\"MO10001000092\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-27T00:00:00\",\"C\",\"1\",122511,\"WH-3100-J\",0,100078,10095,100.0000000000,\"7\",221586,\"1\"],[\"MO10002000078\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-10T00:00:00\",\"C\",\"1\",124123,\"7101-09T-10\",0,100079,10095,15.0000000000,\"7\",0,\"1\"],[\"MO10003000010\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",193574,\"903-1000-M\",0,100080,10095,885.0000000000,\"7\",209209,\"1\"],[\"MO10003000011\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",198700,\"WR7306A(规格:YC-RH)\",0,100080,10095,30.0000000000,\"7\",0,\"1\"],[\"MO10003000011\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",198703,\"WR7306B(规格:YC-RH)\",0,100080,10095,30.0000000000,\"7\",209211,\"1\"],[\"MO10002000083\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-11T00:00:00\",\"C\",\"1\",199617,\"R-15T\",0,100079,10095,10.0000000000,\"7\",0,\"1\"],[\"MO10003000007\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",206101,\"WR7306A(规格:9107L)\",0,100080,10095,180.0000000000,\"7\",0,\"1\"],[\"MO10003000007\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-15T00:00:00\",\"C\",\"1\",206105,\"WR7306B(规格:9107L)\",0,100080,10095,370.0000000000,\"7\",209192,\"1\"],[\"MO10003000030\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-16T00:00:00\",\"C\",\"1\",207920,\"WR7399A规格:D3(高速分散基料)\",0,100080,10095,78.0000000000,\"7\",213449,\"1\"],[\"MO10001000076\",\"123f39178eb2424c8449f992e1fff1ee\",\"2020-09-26T00:00:00\",\"C\",\"1\",216829,\"WH-G310-1A\",0,100078,10095,395.0000000000,\"7\",221203,\"1\"]]";
/**
         * 如果建立了对应的实体类 可以不需要下面的集合装请求的字段
         *  此处是测试请求的数据   所以没有建立最终的实体类
         */
        List<String> key=new ArrayList<String>(){
            {
                this.add("FBillNo");
                this.add("FBillType");
                this.add("FDate");
                this.add("FDocumentStatus");
                this.add("FProductType");
                this.add("FMaterialId");
                this.add("FMaterialName");
                this.add("FWorkShopID0");
                this.add("FPrdOrgId");
                this.add("FUnitId");
                this.add("FQty");
                this.add("FStatus");
                this.add("FLot");
                this.add("FPickMtrlStatus");
            }
        };

        /**
         * 数据处理
         *
         * ==============================================================
         * 获取到的数据 是 String[][]形式的字符串   经过转换之后变成集合
         *      [
         *          [MO10003000080, 123f39178eb2424c8449f992e1fff1ee, 2020-09-21T00:00:00, C, 1, 115084, WR705(规格:JDF), 0, 100080, 10095, 753.0000000000, 7, 0, 1],
         *          [MO10003000012, 123f39178eb2424c8449f992e1fff1ee, 2020-09-15T00:00:00, C, 1, 115098, WR7168(规格:F2), 0, 100080, 10095, 125.0000000000, 7, 0, 1],
         *          [MO10003000080, 123f39178eb2424c8449f992e1fff1ee, 2020-09-21T00:00:00, C, 1, 115169, WR7501PB(规格:7), 0, 100080, 10095, 6.5000000000, 7, 0, 1],
         *          [MO10003000013, 123f39178eb2424c8449f992e1fff1ee, 2020-09-15T00:00:00, C, 1, 115203, WR7501PB(规格:1Y ), 0, 100080, 10095, 10.0000000000, 7, 0, 1],
         *          [MO10003000029, 123f39178eb2424c8449f992e1fff1ee, 2020-09-16T00:00:00, C, 1, 115205, WR7399 A(规格:D3), 0, 100080, 10095, 82.0000000000, 7, 0, 1],
         *          [MO10003000153, 123f39178eb2424c8449f992e1fff1ee, 2020-09-23T00:00:00, C, 1, 115205, WR7399 A(规格:D3), 0, 100080, 10095, 193.0000000000, 7, 0, 1],
         *          [MO10003000031, 123f39178eb2424c8449f992e1fff1ee, 2020-09-16T00:00:00, C, 1, 115226, WR7399B(规格:D5L), 0, 100080, 10095, 99.0000000000, 7, 0, 1],
         *          [MO10003000383, 123f39178eb2424c8449f992e1fff1ee, 2020-09-30T00:00:00, C, 1, 115380, WR7501(规格:T7), 0, 100080, 10095, 20.8800000000, 7, 223760, 1],
         *          [MO10001000076, 123f39178eb2424c8449f992e1fff1ee, 2020-09-26T00:00:00, C, 1, 216829, WH-G310-1A, 0, 100078, 10095, 395.0000000000, 7, 221203, 1]
         *      ]
         */
        String[][] parse1 = JSON.parseObject(str2, String[][].class);
        List<List<String>> lists1 = JSON.parseObject(JSON.toJSONString(parse1),new TypeReference<List<List<String>>>() {});
        System.out.println("lists1");
        lists1.forEach(i-> System.out.println(i));
        System.out.println("----");

        List<Map<String,Object>>listMap=new ArrayList<>();

        /**
         * 此处遍历数据进行处理  以下面形式展现
         *
         * [
         *      {
         *
         *           "FBillNo": "MO10003000080",
         *           "FBillType": "123f39178eb2424c8449f992e1fff1ee",
         *           "FDate": "2020-09-21T00:00:00",
         *           "FDocumentStatus": "C",
         *           "FLot": "0",
         *           "FMaterialId": "115084",
         *           "FMaterialName": "WR705(规格:JDF)",
         *           "FPickMtrlStatus": "1",
         *           "FPrdOrgId": "100080",
         *           "FProductType": "1",
         *           "FQty": "753.0000000000",
         *           "FStatus": "7",
         *           "FUnitId": "10095",
         *           "FWorkShopID0": "0"
         *      },
         *      {
         *           "FBillNo": "MO10001000076",
         *           "FBillType": "123f39178eb2424c8449f992e1fff1ee",
         *           "FDate": "2020-09-26T00:00:00",
         *           "FDocumentStatus": "C",
         *           "FLot": "221203",
         *           "FMaterialId": "216829",
         *           "FMaterialName": "WH-G310-1A",
         *           "FPickMtrlStatus": "1",
         *           "FPrdOrgId": "100078",
         *           "FProductType": "1",
         *           "FQty": "395.0000000000",
         *           "FStatus": "7",
         *           "FUnitId": "10095",
         *           "FWorkShopID0": "0"
         *      },........
         * ]
         *
         *
         */
        lists1.forEach(list->{
            //为了和请求时候传递过去的字段的顺序一样 所以这里需要用有序的Map
            Map<String,Object>map=new LinkedHashMap<>();
            ForEachUtils.forEach(0, list, (index, item) -> {
                map.put( key.get(index),list.get(index));
            });
            listMap.add(map);
        });

        /**
         *转换过后的数据以Json输出
         */
        listMap.forEach(item->{
            System.out.println(item);
            System.out.println(item.get("FBillNo"));

        } );

Tags:

最近发表
标签列表