簡體   English   中英

如何解析 json 鍵值對,其中一個鍵具有 json 值作為字符串

[英]How to parse json key value pair where one of the key having json value as a string

我有如下 json 作為響應,其中我有 json 作為鍵之一中的字符串(即 SubSecData)

"CreatedTimestamp": "2019-10-26T11:28:06.732",
"FromBucket": "AVAILABLE",
"Country": "",
"Process": null,
"InfoId": "",
"ItemsShipped": 0,
"InventoryTypeId": "",
"ItemId": "ITEM02",
"ReasonCodeId": "",
"UpdatedBy": "admin@1",
"SubSecData": "{\"Spec\":\"ReceiptId\",\"Description\":\"Info for 
Receipt\",\"Definition\":{\"Company\":\"***\",\"Season\":\"string\",\"SeasonYear\":\"string\",\"Style\":\"string\",\"StyleSuffix\":\"string\",\"Color\":\"string\",\"ColorSuffix\":\"string\",\"Dimension\":\"string\",\"Code\":\"string\",\"Description\":\"string\",\"ItemId\":\"ITEM02\",\"Description\":\"string\"},\"SubItemFields\":{\"TypeId\":\"\",\"ProcStatusId\":\"\",\"BatchNumber\":\"\",\"Attribute1\":\"\",\"Attribute2\":\"\",\"Attribute3\":\"\",\"InventoryAttribute4\":\"\",\"InventoryAttribute5\":\"\",\"CountryOfOrigin\":\"\",\"ExpirationDate\":\"***\",\"ManufactureDate\":\"***\",\"VendorId\":\"\"},\"InfoFields\":{\"FromBucket\":\"AVAILABLE\",\"ToBucket\":\"AVAILABLE\",\"AdjustmentQty\":\"1\",\"Quantity\":\"1\",\"AdjustmentType\":\"ADD\",\"AdjustedType\":\"ADD\",\"WeightedQty\":\"1.0\",\"WeightedType\":\"ADD\",\"InfoIncId\":\"GHJ686868585\",\"PpnType\":\"IPPN\",\"BWCId\":\"VABWC23969237\"},\"Variances\":{\"\":\"\",\"UnitsReceived\":\"\",\"ItemsShipped\":\"\",\"ItemsReceived\":\"\",\"ReceiptComplete\":\"\"}}",
"TransactionCode": "",

我需要讀取 SubSecData 的數據並使用 java 將它們打印為鍵值對,以便我可以使用預期的鍵值對來斷言它們

我嘗試了下面的代碼並卡住了,不知道如何繼續

public validateNestedJson(expectedKeyValuePairs)
{
    JSONObject getAPIJSONData= new JSONObject(getAPIResponse);
    if (getAPIJSONData.get("SubSecData") != null)
    {
        log.info("Parsing Json Data");
        //iterate expectedKeyValuePairs times
        for(expectedKeyValuePairs=0; expectedKeyValuePairs.length; expectedKeyValuePairs++) {
          //print all the SubSecData elements which match expected KeyValuePairs
          getAPIJSONData.get("SubSecData").<k,v>toHashMap();
        }
          return true;
    }
    else
    {
        return false;
        //print actualvalues - if one or more values doesn't match
    }
}

我將調用“validateNestedJson”方法如下

HashMap<String, String> expectedKeyValuePairs;
expectedKeyValuePairs.put("Description","String");
expectedKeyValuePairs.put("ItemId","ITEM02");
Boolean result = validateNestedJson (expectedKeyValuePairs);

Output 應該為真或假。 如果為假,則需要打印為假的值

SubSecData.Description = Array
SubSecData.ItemId = ITEM01

最后,我能夠想出解決問題的方法。 下面是我使用的代碼。

for (int i = 0; i< jsonArray.length(); i++) {
    JSONObject jsonnew=jsonArray.getJSONObject(i);
    HashMap<HashSet,Object> result = new ObjectMapper().readValue((DataInput) jsonnew, HashMap.class);
    log.info(result.toString());
    log.info(result);
    try
    {
        for (Object key : expectedKeyValuePairs.keySet())
        {
            if (result.containsKey(key))
            {
                result.get(key).equals(expectedKeyValuePairs.get(key));
                log.info("ExpectedKey" + key + "ExpectedValue" + expectedKeyValuePairs.get(key) + "ActualValue" + result.get(key));
            }
        }
    }
    catch (Exception e) {
            //e.printStackTrace((PrintStream) key);
            log.info("failed during wait" + e);
            returnVal = false;
        }
    }
    return returnVal;
}

感謝大家的支持。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM