使用多个数组修改/更新jsonobject内的值?

时间:2016-12-27 10:00:50

标签: java json

我想更改Json中的特定值,有些值正在变化,但数组中的值没有变化。

这是我的Json对象:

    {

  "FileHeader": {

    "FileID": "NY_USGM_1175399504_G",
    "FileType": "Z867MU",
    "FileVersion": "01",
    "Direction": "IN",
    "SubmitterDUNS": "049050839",
    "SubmitterName": "ESG",
    "ReceiverDUNS": "ZNALYTICS",
    "ReceiverName": "ZNALYTICS",
    "FileCreateDate": "2016-12-14T11:26:00+00:00",
    "TransactionCount": 1
  },

  "Transactions": [

    {
      "TransactionHeader": {
        "TransactionSequence": "1",
        "TransactionVersion": "01",
        "PurposeCode": "00",
        "TransactionNumber": "211614160200088161213161205G00",
        "TransactionDate": "20161214",
        "ReferenceNumber": "",
        "MaintTypeCode": "",
        "SubmitterName": "CONSOLIDATED EDISON OF NEW YOR",
        "SubmitterDUNS": "006982359",
        "ReceiverName": "NORTH AMERICAN POWER AND GAS L",
        "ReceiverDUNS": "832461086GAS1",
        "StateCode": "NY"
      },
      "TransactionDetail": {
        "SupplierName": {
          "EntityIdentifierCode": "SJ",
          "EntityName": "NORTH AMERICAN POWER AND GAS L",
          "EntityDUNS": "832461086GAS1",
          "DUNSQualifier": "9"
        },
        "UtilityName": {
          "EntityIdentifierCode": "8S",
          "EntityName": "CONSOLIDATED EDISON OF NEW YOR",
          "EntityDUNS": "006982359",
          "DUNSQualifier": "1"
        },
        "TransactionServiceDetail": {
          "UtilityAccountNumber": "211614160200088",
          "SupplierAccountNumber": "",
          "PreviousUtilityAccountNumber": "",
          "NextMeterReadDate": "20170105",
          "Commodity": "G",
          "ReportTypeCode": "DD"
        },
        "AccountName": {
          "EntityIdentifierCode": "8R",
          "Name": "ALAN LIBERATO        CRYSTAL GARCIA",
          "Address1": "32-32 34 ST 19",
          "City": "ASTORIA",
          "State": "NY",
          "PostalCode": "11106"
        },
        "MeteredSummary": [
          {
            "ProductTransferTypeCode": "PM",
            "ServicePeriodStart": "20160823",
            "ServicePeriodEnd": "20160921",
            "MeterRole": "A",
            "MeterNumber": "127272659",
            "RateClass": "RS",
            "Commodity": "EL",
            "MeterDetail": [
              {
                "QuantityQualifier": "QD",
                "QuantityTotal": "100",
                "UOM": "KH",
                "MeteredDetailMeasurement": [
                  {
                    "MeasurementReferenceCode": "AA",
                    "UOM": "KH",
                    "BeginMeterRead": "6357",
                    "TotalConsumption": "100",
                    "EndMeterRead": "6953",
                    "MeterRegister": "51"
                  }
                ]
              },
              {
                "QuantityQualifier": "87",
                "PMQuantityTotal": "20",
                "UOM": "KH",
                "MeteredDetailMeasurement": [
                  {
                    "MeasurementReferenceCode": "AA",
                    "UOM": "KH",
                    "PMBeginMeterRead": "4078",
                    "PMTotalConsumption": "20",
                    "PMEndMeterRead": "4098",
                    "MeterRegister": "51"
                  }
                ]
              }
            ]
          }
        ],
        "BilledSummary": [
          {
            "ProductTransferTypeCode": "BB",
            "ServicePeriodStart": "20160823",
            "ServicePeriodEnd": "20160921",
            "Commodity": "EL",
            "BilledSummaryDetail": [
              {
                "QuantityQualifier": "D1",
                "BBQuantityTotal": "80",
                "UOM": "KH"
              }
            ]
          }
        ],
        "MeteredUsageSummary": [
          {
            "ProductTransferTypeCode": "SU",
            "ServicePeriodStart": "20160823",
            "ServicePeriodEnd": "20160921",
            "Commodity": "EL",
            "MeteredUsageSummaryDetail": [
              {
                "QuantityQualifier": "QD",
                "QuantityTotal": "134.00000000",
                "UOM": "KH"
              }
            ]
          }
        ]
      }
    }
  ]
}

所以,这里没有将值从"QuantityQualifier": "87"更改为"PMEndMeterRead": "4098"

这是我的Json写代码:

public static void parse(JsonObject object, Hashtable<String, String> listChange) {
    Set<Entry<String, JsonElement>> set = object.entrySet();
    Iterator<Map.Entry<String, JsonElement>> iterator = set.iterator();
    while (iterator.hasNext()) {
      Map.Entry<String, JsonElement> entry = iterator.next();
      JsonElement value = entry.getValue();
      if (value.isJsonObject()) {
        parse(value.getAsJsonObject(), listChange);
      } else if (value.isJsonArray()) {
        parse(value.getAsJsonArray().get(0).getAsJsonObject(), listChange);
        JsonArray jArray = value.getAsJsonArray();
        if (value.getAsJsonArray().size() > 1) {
          parseJsonArray(jArray, listChange);
        }
      } else {
        for (int i = 0; i < listChange.size(); i++) {
          String ekye = entry.getKey();
          String ckey = listChange.keySet().toArray()[i].toString();
          String cvalue = listChange.get(ckey);
          cvalue = cvalue == null ? "" : cvalue;
          if (ekye.equalsIgnoreCase(ckey)) {
            String newValue = "{\"" + ekye + "\":\"" + cvalue + "\"}";
            entry.setValue(new JsonParser().parse(newValue).getAsJsonObject().get(ckey));
          }
        }
      }
    }
  }

我正在使用listChange中的所有更改值。

请尽可能回答这个问题,并随时向我提出一些澄清。

0 个答案:

没有答案