Gson流API未阅读整个文档

v7pvogib  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(178)

我有一个无效的json,但是我仍然要用解析或者其他方法来解析。下面是json:

{"priceAgreementLineDetails":[{"catalogue":{"assetClassDetails":[{}],"assetClassRequired":null,"baseUnitofMeasure":"PMI","bomParent":"Yes","commodityCodeTaric":"84158200","enLanguageKey":"EN","enMaterialLongText":"Lenovo Privacy Filter for 14" Notebooks ( L, T and X1 Carbon)"","grossWeightInKg":null,"height":null,"heightLengthWidthUnit":null,"length":null,"manufacturerPartNumber":"","materialLongDescription":"Lenovo Privacy Filter for 14" Notebooks ( L, T and X1 Carbon)"","physicalCategory":"Physical","volume":null,"volumeUnit":null,"width":null,"xxLanguageKey":null,"xxMaterialLongText":null},"catalogueStatus":"ACTIVE","catalogueStatusTimestamp":"2021-05-11T04:34:52.097Z","comment":null,"commercial":{"appliedVoucher":null,"commitmentDateCategory":null,"discountDateCategory":"custom","manufacturer":"test123","materialDescription":"dsds","materialGroupL4":"K205 - ERP Software & Licenses","netPrice":"7000.00","plannedDeliveryTime":"3","priceAdjustments":null,"priceBookCurrency":"EUR","priceErosionDetails":[{"discount":"5.00","validFrom":"2017-06-01T12:00:00.000Z","validTo":"2022-06-01T12:00:00.000Z"}],"priceUnit":"1","sapContractLineId":"1","supplierPartNumber":"TESTING12321","validFromDate":"2017-06-01T12:00:00.000Z","validToDate":"2022-06-01T12:00:00.000Z","volumeCommitment":null,"volumeDiscounts":null,"volumeTierDiscounts":null},"customGroup":null,"index":null,"isDeleted":false,"isReviewed":true,"lastSaveBy":null,"lastSaveDateTime":null,"lineStatus":"COMPLETE","priceReference":"Purchase Order Date"},{"catalogue":{"assetClassDetails":[{}],"assetClassRequired":null,"baseUnitofMeasure":"PMI","bomParent":"Yes","commodityCodeTaric":"84158200","enLanguageKey":"EN","enMaterialLongText":"(ksjdiusd)","grossWeightInKg":null,"height":null,"heightLengthWidthUnit":null,"length":null,"manufacturerPartNumber":"","materialLongDescription":"(ksjdiusd)","physicalCategory":"Physical","volume":null,"volumeUnit":null,"width":null,"xxLanguageKey":null,"xxMaterialLongText":null},"catalogueStatus":"ACTIVE","catalogueStatusTimestamp":"2021-05-11T04:34:52.097Z","comment":null,"commercial":{"appliedVoucher":null,"commitmentDateCategory":null,"discountDateCategory":null,"manufacturer":"test123","materialDescription":"dsds1","materialGroupL4":"K103 - Customer Management (Billing & CRM) Software & Licenses etc.","netPrice":"5000.00","plannedDeliveryTime":"3","priceAdjustments":null,"priceBookCurrency":"EUR","priceErosionDetails":null,"priceUnit":"1","sapContractLineId":"2","supplierPartNumber":"TESTING123421","validFromDate":"2017-06-01T12:00:00.000Z","validToDate":"2022-06-01T12:00:00.000Z","volumeCommitment":null,"volumeDiscounts":null,"volumeTierDiscounts":null},"customGroup":null,"index":null,"isDeleted":false,"isReviewed":true,"lastSaveBy":null,"lastSaveDateTime":null,"lineStatus":"COMPLETE","priceReference":null},{"catalogue":{"assetClassDetails":[{}],"assetClassRequired":null,"baseUnitofMeasure":"PMI","bomParent":"Yes","commodityCodeTaric":"84158200","enLanguageKey":"EN","enMaterialLongText":"(ks) "di"","grossWeightInKg":null,"height":null,"heightLengthWidthUnit":null,"length":null,"manufacturerPartNumber":"","materialLongDescription":"(ks) "di"","physicalCategory":"Physical","volume":null,"volumeUnit":null,"width":null,"xxLanguageKey":null,"xxMaterialLongText":null},"catalogueStatus":"ACTIVE","catalogueStatusTimestamp":"2021-05-11T04:34:54.353Z","comment":null,"commercial":{"appliedVoucher":null,"commitmentDateCategory":null,"discountDateCategory":null,"manufacturer":"test123","materialDescription":"dsds2","materialGroupL4":"K103 - Customer Management (Billing & CRM) Software & Licenses etc.","netPrice":"5000.00","plannedDeliveryTime":"3","priceAdjustments":null,"priceBookCurrency":"EUR","priceErosionDetails":null,"priceUnit":"1","sapContractLineId":"2","supplierPartNumber":"UHDUSD","validFromDate":"2017-06-01T12:00:00.000Z","validToDate":"2022-06-01T12:00:00.000Z","volumeCommitment":null,"volumeDiscounts":null,"volumeTierDiscounts":null},"customGroup":null,"index":null,"isDeleted":false,"isReviewed":true,"lastSaveBy":null,"lastSaveDateTime":null,"lineStatus":"COMPLETE","priceReference":null},{"catalogue":{"assetClassDetails":[{}],"assetClassRequired":null,"baseUnitofMeasure":"PMI","bomParent":"Yes","commodityCodeTaric":"84158200","enLanguageKey":"EN","enMaterialLongText":"(ksjd , iusd)","grossWeightInKg":null,"height":null,"heightLengthWidthUnit":null,"length":null,"manufacturerPartNumber":"","materialLongDescription":"(ksjd , iusd)","physicalCategory":"Physical","volume":null,"volumeUnit":null,"width":null,"xxLanguageKey":null,"xxMaterialLongText":null},"catalogueStatus":"ACTIVE","catalogueStatusTimestamp":"2021-05-11T04:35:04.574Z","comment":null,"commercial":{"appliedVoucher":null,"commitmentDateCategory":null,"discountDateCategory":null,"manufacturer":"test123","materialDescription":"dsds3","materialGroupL4":"K103 - Customer Management (Billing & CRM) Software & Licenses etc.","netPrice":"6000.00","plannedDeliveryTime":"3","priceAdjustments":null,"priceBookCurrency":"EUR","priceErosionDetails":null,"priceUnit":"1","sapContractLineId":"2","supplierPartNumber":"DSDSDJJ","validFromDate":"2017-06-01T12:00:00.000Z","validToDate":"2022-06-01T12:00:00.000Z","volumeCommitment":null,"volumeDiscounts":null,"volumeTierDiscounts":null},"customGroup":null,"index":null,"isDeleted":false,"isReviewed":true,"lastSaveBy":null,"lastSaveDateTime":null,"lineStatus":"COMPLETE","priceReference":null},{"catalogue":{"assetClassDetails":[{}],"assetClassRequired":null,"baseUnitofMeasure":"PMI","bomParent":"Yes","commodityCodeTaric":"84158200","enLanguageKey":"EN","enMaterialLongText":"(ksjd ) ,( iusd)","grossWeightInKg":null,"height":null,"heightLengthWidthUnit":null,"length":null,"manufacturerPartNumber":"","materialLongDescription":"(ksjd ) ,( iusd)","physicalCategory":"Physical","volume":null,"volumeUnit":null,"width":null,"xxLanguageKey":null,"xxMaterialLongText":null},"catalogueStatus":"ACTIVE","catalogueStatusTimestamp":"2021-05-11T04:34:52.097Z","comment":null,"commercial":{"appliedVoucher":null,"commitmentDateCategory":null,"discountDateCategory":null,"manufacturer":"test123","materialDescription":"dsds4","materialGroupL4":"K103 - Customer Management (Billing & CRM) Software & Licenses etc.","netPrice":"5000.00","plannedDeliveryTime":"3","priceAdjustments":null,"priceBookCurrency":"EUR","priceErosionDetails":null,"priceUnit":"1","sapContractLineId":"2","supplierPartNumber":"TEST1","validFromDate":"2017-06-01T12:00:00.000Z","validToDate":"2022-06-01T12:00:00.000Z","volumeCommitment":null,"volumeDiscounts":null,"volumeTierDiscounts":null},"customGroup":null,"index":null,"isDeleted":false,"isReviewed":true,"lastSaveBy":null,"lastSaveDateTime":null,"lineStatus":"COMPLETE","priceReference":null},{"catalogue":{"assetClassDetails":[{}],"assetClassRequired":null,"baseUnitofMeasure":"PMI","bomParent":"Yes","commodityCodeTaric":"84158200","enLanguageKey":"EN","enMaterialLongText":"(ksjd) & " iusd"","grossWeightInKg":null,"height":null,"heightLengthWidthUnit":null,"length":null,"manufacturerPartNumber":"","materialLongDescription":"(ksjd) & " iusd"","physicalCategory":"Physical","volume":null,"volumeUnit":null,"width":null,"xxLanguageKey":null,"xxMaterialLongText":null},"catalogueStatus":"ACTIVE","catalogueStatusTimestamp":"2021-05-11T04:34:52.097Z","comment":null,"commercial":{"appliedVoucher":null,"commitmentDateCategory":null,"discountDateCategory":null,"manufacturer":"test123","materialDescription":"dsds5","materialGroupL4":"K103 - Customer Management (Billing & CRM) Software & Licenses etc.","netPrice":"5000.00","plannedDeliveryTime":"3","priceAdjustments":null,"priceBookCurrency":"EUR","priceErosionDetails":null,"priceUnit":"1","sapContractLineId":"2","supplierPartNumber":"1233","validFromDate":"2017-06-01T12:00:00.000Z","validToDate":"2022-06-01T12:00:00.000Z","volumeCommitment":null,"volumeDiscounts":null,"volumeTierDiscounts":null},"customGroup":null,"index":null,"isDeleted":false,"isReviewed":true,"lastSaveBy":null,"lastSaveDateTime":null,"lineStatus":"COMPLETE","priceReference":null}],"priceAgreementStatusTimestamp":"2021-05-11T04:36:52.000Z","priceAgreementStatus":"APPROVED","lastSaveBy":"cba089b2-807e-4924-806a-3105aa46cd70","lastSaveByOrganisation":"vodafoneMSP","lastSubmittedByOrganisation":"vodafoneMSP","lastSubmittedDateTime":"2021-05-11T04:35:14.000Z","lastSaveDateTime":"2021-05-11T04:36:52.000Z","version":"2","catalogueStatusTimestamp":"2021-05-11T04:36:52.000Z","catalogueStatus":"ACTIVE","exchangeRate":null,"conversionRateReference":null,"conversionChangeFrequency":null,"createdBy":"cba089b2-807e-4924-806a-3105aa46cd70","createdByOrganization":"vodafoneMSP","action":null,"creationTimestamp":"2021-05-10T11:14:12.000Z","type":"priceAgreement","counter":"8","requestForRevision":false,"materialCodeList":["K205 - ERP Software & Licenses","K103 - Customer Management (Billing & CRM) Software & Licenses etc."],"masterAgreement":{"type":"masterAgreement","catalogueType":"IT&E","duplicationCode":"ES00","masterAgreementId":"ERP2017MA005108","masterAgreementName":"NA","materialL4Details":[{"materialGroupL4Desc":"K205 - ERP Software & Licenses","materialGroupL4Id":"K205","olaLineItemId":"1","sirionLineItemId":null},{"materialGroupL4Desc":"K103 - Customer Management (Billing & CRM) Software & Licenses etc.","materialGroupL4Id":"K103","olaLineItemId":"2","sirionLineItemId":null},{"materialGroupL4Desc":"B218 - Customer Management (Billing & CRM) Systems & Software Maintenance, Support incl. SaaS","materialGroupL4Id":"B218","olaLineItemId":"4","sirionLineItemId":null}],"opcoCode":"LU68","opcoName":"Spain","organizationId":"oracle","outlineAgreementNumber":"1250027399","parentSupplier":"ORACLE CORPORATION UK LIMITED","purchasingIncoterm":"-","purchasingOrg":"LU07 - VF POrg Luxembourg","purchasingOrgCode":"LU07","supplierId":"0400017770","supplierName":"ORACLE CORPORATION UK LIMITED","tradingCurrencyKey":"EUR","tradingModel":"Buy From","validFromDate":"2017-06-01T00:00:00.000Z","validToDate":"2022-06-01T00:00:00.000Z","priceAgreementMetadata":{"catalogueType":"IT&E","catalogueName":"VPC_BF_IT_ORA_VFES_ICT_400017770_D76A18D","catalogueDescription":null,"priceAgreementReferenceName":"VPC_BF_IT_ORA_VFES_ICT_400017770_D76A18D","priceAgreementSupplierReferenceName":"VPC_BF_IT_ORA_VFES_ICT_400017770_D76A18D","catalogueGroup":"ES IT Catalogues - ES_IT_mask","initialCategoryManagerEmail":"cba089b2-807e-4924-806a-3105aa46cd70","initialSupplierManagerEmail":"fa52bb7d-63d8-43c6-8908-39387edb39f1"}},"priceAgreementId":"VPC_BF_IT_ORA_VFES_ICT_400017770_D76A18D"}

这是我的java 8代码:

public static void main(String aa[]) {
    String test = "{\"priceAgreementLineDetails\":[{\"catalogue\":{\"assetClassDetails\":[{}],\"assetClassRequired\":null,\"baseUnitofMeasure\":\"PMI\",\"bomParent\":\"Yes\",\"commodityCodeTaric\":\"84158200\",\"enLanguageKey\":\"EN\",\"enMaterialLongText\":\"Lenovo Privacy Filter for 14\" Notebooks ( L, T and X1 Carbon)\"\",\"grossWeightInKg\":null,\"height\":null,\"heightLengthWidthUnit\":null,\"length\":null,\"manufacturerPartNumber\":\"\",\"materialLongDescription\":\"Lenovo Privacy Filter for 14\" Notebooks ( L, T and X1 Carbon)\"\",\"physicalCategory\":\"Physical\",\"volume\":null,\"volumeUnit\":null,\"width\":null,\"xxLanguageKey\":null,\"xxMaterialLongText\":null},\"catalogueStatus\":\"ACTIVE\",\"catalogueStatusTimestamp\":\"2021-05-11T04:34:52.097Z\",\"comment\":null,\"commercial\":{\"appliedVoucher\":null,\"commitmentDateCategory\":null,\"discountDateCategory\":\"custom\",\"manufacturer\":\"test123\",\"materialDescription\":\"dsds\",\"materialGroupL4\":\"K205 - ERP Software & Licenses\",\"netPrice\":\"7000.00\",\"plannedDeliveryTime\":\"3\",\"priceAdjustments\":null,\"priceBookCurrency\":\"EUR\",\"priceErosionDetails\":[{\"discount\":\"5.00\",\"validFrom\":\"2017-06-01T12:00:00.000Z\",\"validTo\":\"2022-06-01T12:00:00.000Z\"}],\"priceUnit\":\"1\",\"sapContractLineId\":\"1\",\"supplierPartNumber\":\"TESTING12321\",\"validFromDate\":\"2017-06-01T12:00:00.000Z\",\"validToDate\":\"2022-06-01T12:00:00.000Z\",\"volumeCommitment\":null,\"volumeDiscounts\":null,\"volumeTierDiscounts\":null},\"customGroup\":null,\"index\":null,\"isDeleted\":false,\"isReviewed\":true,\"lastSaveBy\":null,\"lastSaveDateTime\":null,\"lineStatus\":\"COMPLETE\",\"priceReference\":\"Purchase Order Date\"},{\"catalogue\":{\"assetClassDetails\":[{}],\"assetClassRequired\":null,\"baseUnitofMeasure\":\"PMI\",\"bomParent\":\"Yes\",\"commodityCodeTaric\":\"84158200\",\"enLanguageKey\":\"EN\",\"enMaterialLongText\":\"(ksjdiusd)\",\"grossWeightInKg\":null,\"height\":null,\"heightLengthWidthUnit\":null,\"length\":null,\"manufacturerPartNumber\":\"\",\"materialLongDescription\":\"(ksjdiusd)\",\"physicalCategory\":\"Physical\",\"volume\":null,\"volumeUnit\":null,\"width\":null,\"xxLanguageKey\":null,\"xxMaterialLongText\":null},\"catalogueStatus\":\"ACTIVE\",\"catalogueStatusTimestamp\":\"2021-05-11T04:34:52.097Z\",\"comment\":null,\"commercial\":{\"appliedVoucher\":null,\"commitmentDateCategory\":null,\"discountDateCategory\":null,\"manufacturer\":\"test123\",\"materialDescription\":\"dsds1\",\"materialGroupL4\":\"K103 - Customer Management (Billing & CRM) Software & Licenses etc.\",\"netPrice\":\"5000.00\",\"plannedDeliveryTime\":\"3\",\"priceAdjustments\":null,\"priceBookCurrency\":\"EUR\",\"priceErosionDetails\":null,\"priceUnit\":\"1\",\"sapContractLineId\":\"2\",\"supplierPartNumber\":\"TESTING123421\",\"validFromDate\":\"2017-06-01T12:00:00.000Z\",\"validToDate\":\"2022-06-01T12:00:00.000Z\",\"volumeCommitment\":null,\"volumeDiscounts\":null,\"volumeTierDiscounts\":null},\"customGroup\":null,\"index\":null,\"isDeleted\":false,\"isReviewed\":true,\"lastSaveBy\":null,\"lastSaveDateTime\":null,\"lineStatus\":\"COMPLETE\",\"priceReference\":null},{\"catalogue\":{\"assetClassDetails\":[{}],\"assetClassRequired\":null,\"baseUnitofMeasure\":\"PMI\",\"bomParent\":\"Yes\",\"commodityCodeTaric\":\"84158200\",\"enLanguageKey\":\"EN\",\"enMaterialLongText\":\"(ks) \"di\"\",\"grossWeightInKg\":null,\"height\":null,\"heightLengthWidthUnit\":null,\"length\":null,\"manufacturerPartNumber\":\"\",\"materialLongDescription\":\"(ks) \"di\"\",\"physicalCategory\":\"Physical\",\"volume\":null,\"volumeUnit\":null,\"width\":null,\"xxLanguageKey\":null,\"xxMaterialLongText\":null},\"catalogueStatus\":\"ACTIVE\",\"catalogueStatusTimestamp\":\"2021-05-11T04:34:54.353Z\",\"comment\":null,\"commercial\":{\"appliedVoucher\":null,\"commitmentDateCategory\":null,\"discountDateCategory\":null,\"manufacturer\":\"test123\",\"materialDescription\":\"dsds2\",\"materialGroupL4\":\"K103 - Customer Management (Billing & CRM) Software & Licenses etc.\",\"netPrice\":\"5000.00\",\"plannedDeliveryTime\":\"3\",\"priceAdjustments\":null,\"priceBookCurrency\":\"EUR\",\"priceErosionDetails\":null,\"priceUnit\":\"1\",\"sapContractLineId\":\"2\",\"supplierPartNumber\":\"UHDUSD\",\"validFromDate\":\"2017-06-01T12:00:00.000Z\",\"validToDate\":\"2022-06-01T12:00:00.000Z\",\"volumeCommitment\":null,\"volumeDiscounts\":null,\"volumeTierDiscounts\":null},\"customGroup\":null,\"index\":null,\"isDeleted\":false,\"isReviewed\":true,\"lastSaveBy\":null,\"lastSaveDateTime\":null,\"lineStatus\":\"COMPLETE\",\"priceReference\":null},{\"catalogue\":{\"assetClassDetails\":[{}],\"assetClassRequired\":null,\"baseUnitofMeasure\":\"PMI\",\"bomParent\":\"Yes\",\"commodityCodeTaric\":\"84158200\",\"enLanguageKey\":\"EN\",\"enMaterialLongText\":\"(ksjd , iusd)\",\"grossWeightInKg\":null,\"height\":null,\"heightLengthWidthUnit\":null,\"length\":null,\"manufacturerPartNumber\":\"\",\"materialLongDescription\":\"(ksjd , iusd)\",\"physicalCategory\":\"Physical\",\"volume\":null,\"volumeUnit\":null,\"width\":null,\"xxLanguageKey\":null,\"xxMaterialLongText\":null},\"catalogueStatus\":\"ACTIVE\",\"catalogueStatusTimestamp\":\"2021-05-11T04:35:04.574Z\",\"comment\":null,\"commercial\":{\"appliedVoucher\":null,\"commitmentDateCategory\":null,\"discountDateCategory\":null,\"manufacturer\":\"test123\",\"materialDescription\":\"dsds3\",\"materialGroupL4\":\"K103 - Customer Management (Billing & CRM) Software & Licenses etc.\",\"netPrice\":\"6000.00\",\"plannedDeliveryTime\":\"3\",\"priceAdjustments\":null,\"priceBookCurrency\":\"EUR\",\"priceErosionDetails\":null,\"priceUnit\":\"1\",\"sapContractLineId\":\"2\",\"supplierPartNumber\":\"DSDSDJJ\",\"validFromDate\":\"2017-06-01T12:00:00.000Z\",\"validToDate\":\"2022-06-01T12:00:00.000Z\",\"volumeCommitment\":null,\"volumeDiscounts\":null,\"volumeTierDiscounts\":null},\"customGroup\":null,\"index\":null,\"isDeleted\":false,\"isReviewed\":true,\"lastSaveBy\":null,\"lastSaveDateTime\":null,\"lineStatus\":\"COMPLETE\",\"priceReference\":null},{\"catalogue\":{\"assetClassDetails\":[{}],\"assetClassRequired\":null,\"baseUnitofMeasure\":\"PMI\",\"bomParent\":\"Yes\",\"commodityCodeTaric\":\"84158200\",\"enLanguageKey\":\"EN\",\"enMaterialLongText\":\"(ksjd ) ,( iusd)\",\"grossWeightInKg\":null,\"height\":null,\"heightLengthWidthUnit\":null,\"length\":null,\"manufacturerPartNumber\":\"\",\"materialLongDescription\":\"(ksjd ) ,( iusd)\",\"physicalCategory\":\"Physical\",\"volume\":null,\"volumeUnit\":null,\"width\":null,\"xxLanguageKey\":null,\"xxMaterialLongText\":null},\"catalogueStatus\":\"ACTIVE\",\"catalogueStatusTimestamp\":\"2021-05-11T04:34:52.097Z\",\"comment\":null,\"commercial\":{\"appliedVoucher\":null,\"commitmentDateCategory\":null,\"discountDateCategory\":null,\"manufacturer\":\"test123\",\"materialDescription\":\"dsds4\",\"materialGroupL4\":\"K103 - Customer Management (Billing & CRM) Software & Licenses etc.\",\"netPrice\":\"5000.00\",\"plannedDeliveryTime\":\"3\",\"priceAdjustments\":null,\"priceBookCurrency\":\"EUR\",\"priceErosionDetails\":null,\"priceUnit\":\"1\",\"sapContractLineId\":\"2\",\"supplierPartNumber\":\"TEST1\",\"validFromDate\":\"2017-06-01T12:00:00.000Z\",\"validToDate\":\"2022-06-01T12:00:00.000Z\",\"volumeCommitment\":null,\"volumeDiscounts\":null,\"volumeTierDiscounts\":null},\"customGroup\":null,\"index\":null,\"isDeleted\":false,\"isReviewed\":true,\"lastSaveBy\":null,\"lastSaveDateTime\":null,\"lineStatus\":\"COMPLETE\",\"priceReference\":null},{\"catalogue\":{\"assetClassDetails\":[{}],\"assetClassRequired\":null,\"baseUnitofMeasure\":\"PMI\",\"bomParent\":\"Yes\",\"commodityCodeTaric\":\"84158200\",\"enLanguageKey\":\"EN\",\"enMaterialLongText\":\"(ksjd) & \" iusd\"\",\"grossWeightInKg\":null,\"height\":null,\"heightLengthWidthUnit\":null,\"length\":null,\"manufacturerPartNumber\":\"\",\"materialLongDescription\":\"(ksjd) & \" iusd\"\",\"physicalCategory\":\"Physical\",\"volume\":null,\"volumeUnit\":null,\"width\":null,\"xxLanguageKey\":null,\"xxMaterialLongText\":null},\"catalogueStatus\":\"ACTIVE\",\"catalogueStatusTimestamp\":\"2021-05-11T04:34:52.097Z\",\"comment\":null,\"commercial\":{\"appliedVoucher\":null,\"commitmentDateCategory\":null,\"discountDateCategory\":null,\"manufacturer\":\"test123\",\"materialDescription\":\"dsds5\",\"materialGroupL4\":\"K103 - Customer Management (Billing & CRM) Software & Licenses etc.\",\"netPrice\":\"5000.00\",\"plannedDeliveryTime\":\"3\",\"priceAdjustments\":null,\"priceBookCurrency\":\"EUR\",\"priceErosionDetails\":null,\"priceUnit\":\"1\",\"sapContractLineId\":\"2\",\"supplierPartNumber\":\"1233\",\"validFromDate\":\"2017-06-01T12:00:00.000Z\",\"validToDate\":\"2022-06-01T12:00:00.000Z\",\"volumeCommitment\":null,\"volumeDiscounts\":null,\"volumeTierDiscounts\":null},\"customGroup\":null,\"index\":null,\"isDeleted\":false,\"isReviewed\":true,\"lastSaveBy\":null,\"lastSaveDateTime\":null,\"lineStatus\":\"COMPLETE\",\"priceReference\":null}],\"priceAgreementStatusTimestamp\":\"2021-05-11T04:36:52.000Z\",\"priceAgreementStatus\":\"APPROVED\",\"lastSaveBy\":\"cba089b2-807e-4924-806a-3105aa46cd70\",\"lastSaveByOrganisation\":\"vodafoneMSP\",\"lastSubmittedByOrganisation\":\"vodafoneMSP\",\"lastSubmittedDateTime\":\"2021-05-11T04:35:14.000Z\",\"lastSaveDateTime\":\"2021-05-11T04:36:52.000Z\",\"version\":\"2\",\"catalogueStatusTimestamp\":\"2021-05-11T04:36:52.000Z\",\"catalogueStatus\":\"ACTIVE\",\"exchangeRate\":null,\"conversionRateReference\":null,\"conversionChangeFrequency\":null,\"createdBy\":\"cba089b2-807e-4924-806a-3105aa46cd70\",\"createdByOrganization\":\"vodafoneMSP\",\"action\":null,\"creationTimestamp\":\"2021-05-10T11:14:12.000Z\",\"type\":\"priceAgreement\",\"counter\":\"8\",\"requestForRevision\":false,\"materialCodeList\":[\"K205 - ERP Software & Licenses\",\"K103 - Customer Management (Billing & CRM) Software & Licenses etc.\"],\"masterAgreement\":{\"type\":\"masterAgreement\",\"catalogueType\":\"IT&E\",\"duplicationCode\":\"ES00\",\"masterAgreementId\":\"ERP2017MA005108\",\"masterAgreementName\":\"NA\",\"materialL4Details\":[{\"materialGroupL4Desc\":\"K205 - ERP Software & Licenses\",\"materialGroupL4Id\":\"K205\",\"olaLineItemId\":\"1\",\"sirionLineItemId\":null},{\"materialGroupL4Desc\":\"K103 - Customer Management (Billing & CRM) Software & Licenses etc.\",\"materialGroupL4Id\":\"K103\",\"olaLineItemId\":\"2\",\"sirionLineItemId\":null},{\"materialGroupL4Desc\":\"B218 - Customer Management (Billing & CRM) Systems & Software Maintenance, Support incl. SaaS\",\"materialGroupL4Id\":\"B218\",\"olaLineItemId\":\"4\",\"sirionLineItemId\":null}],\"opcoCode\":\"LU68\",\"opcoName\":\"Spain\",\"organizationId\":\"oracle\",\"outlineAgreementNumber\":\"1250027399\",\"parentSupplier\":\"ORACLE CORPORATION UK LIMITED\",\"purchasingIncoterm\":\"-\",\"purchasingOrg\":\"LU07 - VF POrg Luxembourg\",\"purchasingOrgCode\":\"LU07\",\"supplierId\":\"0400017770\",\"supplierName\":\"ORACLE CORPORATION UK LIMITED\",\"tradingCurrencyKey\":\"EUR\",\"tradingModel\":\"Buy From\",\"validFromDate\":\"2017-06-01T00:00:00.000Z\",\"validToDate\":\"2022-06-01T00:00:00.000Z\",\"priceAgreementMetadata\":{\"catalogueType\":\"IT&E\",\"catalogueName\":\"VPC_BF_IT_ORA_VFES_ICT_400017770_D76A18D\",\"catalogueDescription\":null,\"priceAgreementReferenceName\":\"VPC_BF_IT_ORA_VFES_ICT_400017770_D76A18D\",\"priceAgreementSupplierReferenceName\":\"VPC_BF_IT_ORA_VFES_ICT_400017770_D76A18D\",\"catalogueGroup\":\"ES IT Catalogues - ES_IT_mask\",\"initialCategoryManagerEmail\":\"cba089b2-807e-4924-806a-3105aa46cd70\",\"initialSupplierManagerEmail\":\"fa52bb7d-63d8-43c6-8908-39387edb39f1\"}},\"priceAgreementId\":\"VPC_BF_IT_ORA_VFES_ICT_400017770_D76A18D\"}";
    JsonReader jsonReader = new JsonReader(new StringReader(test));
    jsonReader.setLenient(true);
    try
    {
        while (jsonReader.hasNext()) 
        {
            JsonToken nextToken = jsonReader.peek();

            if (JsonToken.BEGIN_OBJECT.equals(nextToken)) {
                System.out.println("Begin object");
                jsonReader.beginObject();

            } else if (JsonToken.NAME.equals(nextToken)) {

                String name = jsonReader.nextName();
                System.out.println("Token KEY >>>> " + name);

            } else if (JsonToken.STRING.equals(nextToken)) {

                String value = jsonReader.nextString();
                System.out.println("Token Value >>>> " + value);

            } else if (JsonToken.NUMBER.equals(nextToken)) {

                long value = jsonReader.nextLong();
                System.out.println("Token Value >>>> " + value);

            } else if (JsonToken.NULL.equals(nextToken)) {

                jsonReader.nextNull();
                System.out.println("Token Value >>>> null");

            } else if (JsonToken.END_OBJECT.equals(nextToken)) {
                System.out.println("End object");
                jsonReader.endObject();

            }
            else if (JsonToken.BEGIN_ARRAY.equals(nextToken)) {
                System.out.println("Begin array");
                jsonReader.beginArray();
            }
            else if (JsonToken.END_ARRAY.equals(nextToken)) {
                System.out.println("End array");
                jsonReader.endArray();

            }
            else if(JsonToken.END_DOCUMENT.equals(nextToken)) {
                System.out.println("End document");
            }

        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            jsonReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

它给出的输出为:

Begin object
Token KEY >>>> priceAgreementLineDetails
Begin array
Begin object
Token KEY >>>> catalogue
Begin object
Token KEY >>>> assetClassDetails
Begin array
Begin object

所以当它遇到一个空对象时,它就不再前进了。因为这是一个无效的json,我希望它会失败,但它失败得太早了。

2admgd59

2admgd591#

您的算法的工作方式是,一旦代码到达ANY END_OBJECT标记,它就会分离出代码中唯一的非嵌套while。(即数组和对象),因此,assetClassDetails中的第一个空对象不能与顶部对象末端区分开。使用堆栈来保存每个子元素的状态,或者使用递归:

public final class StreamReadingTest {

    @Test
    public void test()
            throws IOException {
        try ( final JsonReader jsonReader = ... ) {
            jsonReader.setLenient(true);
            read(jsonReader);
        }
    }

    private static void read(final JsonReader jsonReader)
            throws IOException {
        read(jsonReader, 0);
    }

    private static void read(final JsonReader jsonReader, final int level)
            throws IOException {
        final JsonToken token = jsonReader.peek();
        switch ( token ) {
        case BEGIN_ARRAY:
            printMessage(level, "ARRAY");
            jsonReader.beginArray();
            while ( jsonReader.hasNext() ) {
                read(jsonReader, level + 1);
            }
            jsonReader.endArray();
            break;
        case END_ARRAY:
            throw new AssertionError();
        case BEGIN_OBJECT:
            printMessage(level, "OBJECT");
            jsonReader.beginObject();
            while ( jsonReader.hasNext() ) {
                printMessage(level, "| NAME = " + jsonReader.nextName());
                read(jsonReader, level + 1);
            }
            jsonReader.endObject();
            break;
        case END_OBJECT:
            throw new AssertionError();
        case NAME:
            throw new AssertionError();
        case STRING:
            printMessage(level, "STRING " + jsonReader.nextString());
            break;
        case NUMBER:
            printMessage(level, "NUMBER " + jsonReader.nextString()); // reading a string for not losing a precision
            break;
        case BOOLEAN:
            printMessage(level, "BOOLEAN " + jsonReader.nextBoolean());
            break;
        case NULL:
            jsonReader.nextNull();
            printMessage(level, "NULL");
            break;
        case END_DOCUMENT:
            printMessage(level, "END");
            break;
        default:
            throw new AssertionError(token);
        }
    }

    private static void printMessage(final int level, final String message) {
        for ( int i = 0; i < level; i++ ) {
            System.out.print("    ");
        }
        System.out.println(message);
    }

}

运行后,将打印:

OBJECT
| NAME = priceAgreementLineDetails
    ARRAY
        OBJECT
        | NAME = catalogue
            OBJECT
            | NAME = assetClassDetails
                ARRAY
                    OBJECT
            | NAME = assetClassRequired
                NULL
            | NAME = baseUnitofMeasure
                STRING PMI
            | NAME = bomParent
                STRING Yes
            | NAME = commodityCodeTaric
                STRING 84158200
            | NAME = enLanguageKey
                STRING EN
            | NAME = enMaterialLongText
                STRING Lenovo Privacy Filter for 14

com.google.gson.stream.MalformedJsonException: Unterminated object at line 1 column 246 path $.priceAgreementLineDetails[0].catalogue.enMaterialLongText

因为JSON是无效的。

相关问题