如何解析json键值对,其中键之一具有json值作为字符串

问题描述 投票:-2回答:1

我将json作为响应,如下所示,在其中一个键(即SubSecData)中,我将json作为字符串。

"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);

输出应为true或false。如果为false,则需要打印为false的值

SubSecData.Description = Array
SubSecData.ItemId = ITEM01
java json
1个回答
0
投票

最后,我能够为该问题提供解决方案。下面是我使用的代码。

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;
}

感谢大家的支持。

© www.soinside.com 2019 - 2024. All rights reserved.