0

我目前正在尝试使用 RestAssured JsonPath 解析 JSON 响应正文中的嵌套 json。我正在尝试解析此嵌套 json 中的元素,并将每个特定元素添加到基于索引的列表中。下面是json响应的结构:

{
    "responseCd": "0",
    "responseTxt": "Success",
    "accGraphResponse": [
        {
            "lag": 0,
            "mape_BY": 86.74007669775958,
            "bias_LIFT": 30.82309403061364,
            "bias_BY": 4.784044251721182,
            "fsc_WK_NBR": 8,
            "mae_LIFT": 0.7950708365748616,
            "fsc_YR_NBR": 2021,
            "mape_LIFT": 120.86528308904767,
            "mae_BY": 1.322740404547543
        },
        {
            "lag": 1,
            "mape_BY": 82.36383172317584,
            "bias_LIFT": 70.14411879636555,
            "bias_BY": -9.201951869230239,
            "fsc_WK_NBR": 8,
            "mae_LIFT": 0.9178292771207154,
            "fsc_YR_NBR": 2021,
            "mape_LIFT": 140.27481258292696,
            "mae_BY": 1.2618460034136607
        },
        {
            "lag": 2,
            "mape_BY": 81.68821133500256,
            "bias_LIFT": -5.00098100566803,
            "bias_BY": -9.703882478412659,
            "fsc_WK_NBR": 8,
            "mae_LIFT": 0.7097345157634638,
            "fsc_YR_NBR": 2021,
            "mape_LIFT": 94.99387451700188,
            "mae_BY": 1.2344427973680134
        },
        {
            "lag": 8,
            "mape_BY": 116.02384185546313,
            "bias_LIFT": -5.942062107805376,
            "bias_BY": 7.173228517669925,
            "fsc_WK_NBR": 34,
            "mae_LIFT": 0.7158352741362445,
            "fsc_YR_NBR": 2021,
            "mape_LIFT": 104.4479660772388,
            "mae_BY": 0.7951706649752057
        }
    ],
    "accTableResponse": [
        {
            "subdivision": 119,
            "lag": 0,
            "mape_BY": 93.7047364051686,
            "bias_LIFT": 4.089475958442945,
            "bias_BY": -7.311870773687033,
            "fsc_WK_NBR": 34,
            "avg_FORECAST_UNITS_LIFT": 0.7760853555600128,
            "mae_LIFT": 0.6888312594850159,
            "fsc_YR_NBR": 2021,
            "avg_FORECAST_UNITS_BY": 0.6869555354597405,
            "mape_LIFT": 92.3876113387987,
            "tot_FORECAST_UNITS_LIFT": 364056.98378106864,
            "mae_BY": 0.6986515904278708,
            "tot_FORECAST_UNITS_BY": 313733.279999999,
            "avg_SALES_UNITS": 0.7455883418816537,
            "tot_SALES_UNITS": 349754,
            "fsc_WK_END_DT": "2021-09-24T04:00:00.000+00:00"
        },
        {
            "subdivision": 119,
            "lag": 1,
            "mape_BY": 93.48231529697387,
            "bias_LIFT": 3.968069781435852,
            "bias_BY": -9.624993375750748,
            "fsc_WK_NBR": 34,
            "avg_FORECAST_UNITS_LIFT": 0.7749491435314203,
            "mae_LIFT": 0.7072070519574567,
            "fsc_YR_NBR": 2021,
            "avg_FORECAST_UNITS_BY": 0.669872842929802,
            "mape_LIFT": 94.85221431610191,
            "tot_FORECAST_UNITS_LIFT": 363461.99760424555,
            "mae_BY": 0.696993244575287,
            "tot_FORECAST_UNITS_BY": 305811.6899999991,
            "avg_SALES_UNITS": 0.7455883418816537,
            "tot_SALES_UNITS": 349754,
            "fsc_WK_END_DT": "2021-09-24T04:00:00.000+00:00"
        },
        {
            "subdivision": 237,
            "lag": 2,
            "mape_BY": 144.18640321046288,
            "bias_LIFT": 3.427337326567681,
            "bias_BY": 73.05687809126792,
            "fsc_WK_NBR": 34,
            "avg_FORECAST_UNITS_LIFT": 1.5013061858643058,
            "mae_LIFT": 1.3146445954539658,
            "fsc_YR_NBR": 2021,
            "avg_FORECAST_UNITS_BY": 2.0119278895673474,
            "mape_LIFT": 122.76798525431151,
            "tot_FORECAST_UNITS_LIFT": 254918.7877473874,
            "mae_BY": 1.5440008673753518,
            "tot_FORECAST_UNITS_BY": 408242.3,
            "avg_SALES_UNITS": 1.0708366621238468,
            "tot_SALES_UNITS": 273436,
            "fsc_WK_END_DT": "2021-09-24T04:00:00.000+00:00"
        },
        {
            "subdivision": 146,
            "lag": 8,
            "mape_BY": 114.18761776449284,
            "bias_LIFT": 10.045590334452834,
            "bias_BY": -3.0668036363500337,
            "fsc_WK_NBR": 34,
            "avg_FORECAST_UNITS_LIFT": 0.4599414391163643,
            "mae_LIFT": 0.48090834480008454,
            "fsc_YR_NBR": 2021,
            "avg_FORECAST_UNITS_BY": 0.4087242243975078,
            "mape_LIFT": 115.38718677880458,
            "tot_FORECAST_UNITS_LIFT": 221231.37227353212,
            "mae_BY": 0.47590880572429445,
            "tot_FORECAST_UNITS_BY": 194783.6599999983,
            "avg_SALES_UNITS": 0.4167779440900819,
            "tot_SALES_UNITS": 201315,
            "fsc_WK_END_DT": "2021-09-24T04:00:00.000+00:00"
        }
    ]
}

我的方法如下所示:

public void getAccTableResponseProdLocCols(String filterMapping) {
    List<Object> prodHieData = new ArrayList<Object>();
    List<Object> maeBYData = new ArrayList<Object>();
    //response already part of a base class with code not added here for clarity of the problem
    String response = gResponse.asString();
    String str = response.split(":")[1].replace("}", "");
    try {
        if (!str.equals("[]") || !str.isEmpty()) {
            JsonPath json = new JsonPath(response);
            List tableResponseData = new ArrayList<Object>();
            //accTableResponse
            tableResponseData = json.get("accTableResponse");
            if (tableResponseData.size() > 0) {
                for (int j = 0; j < tableResponseData.size(); j++) {
                    //switch based on filtered mapping which dictates the result to be added to prodHieData and also add all the other fields to the respective list (such as mae_BY added to maeBYData)
                    switch (filterMapping) {
                        case "Merch_MerchDivision_Natl":
                            productHieData.add(json.getList("accTableResponse.subdivision"));
                            maeBYData.add(json.getList("accTableResponse.mae_BY"));
                            break;
                        //case "xxxx"
                        
                        //case "yyyy"
                    default:
                        Reporter.log("Invalid columns", true);
                    }
                }
            }
        } 
        else {
            Reporter.log("There is no table data for the selected combinations", true);
        }
    }
    catch (Exception e) {
        Reporter.log("The response is empty");
    }
}

我希望能够根据索引位置添加到列表中(它会在不同的响应中发生变化,即,而不是“细分”,另一个 filterMapping 字段可以是除法作为示例)。我正在寻找专门获取accTableResponse和其中的响应正文。当我基于字符串 (filterMapping) 执行切换时,我还希望能够在其索引 (j) 处获取该值并正确存储在列表中。如何在这个嵌套的 json 结构中实现这一点?请让我知道任何建议!

4

0 回答 0