【问题标题】:How to traverse through inner Json array and retrieve the values如何遍历内部 Json 数组并检索值
【发布时间】:2021-03-22 16:17:12
【问题描述】:

我在下面给出了一个响应 Json 我需要从内部“childItems”数组中检索价格值并将其相加得到总价格 = 7.7

下面是我刚刚尝试的java代码,我猜有点错误。

List<Float> items_price = response.jsonPath().getList("items.childItems.price");
   float Total_Price=0;
   for(int i=0;i<items_price.size();i++)
   {
      Total_Price = Total_Price + items_price.get(i);
   }
   Total_Price = Total_Price + response.jsonPath().getFloat("tax");

下面是响应 Json ===========================

{
      "brandId": "HAL",
      "sellingChannel": "WEBOA",
      "subTotal": 7.19,
      "total": 7.7,
      "items": [
        {
          "lineItemId": 101,
          "description": "Apple Buffet",
          "price": 0,
          "priceType": null,
          "productId": "HAL-itm-000-057",
          "childItems": [
            {
              "lineItemId": 102,
              "description": "Apple",
              "price": 3.31,
              "priceType": null,
              "productId": "HAL-itm-000-055",
              "childItems": [],
              "modifierGroups": [
                {
                  "description": null,
                  "productId": "HAL-prg-001-001",
                  "modifiers": [
                    {
                      "productId": "HAL-itm-006-011",
                      "price": 2,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    }
                  ]
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-002",
                  "modifiers": []
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-003",
                  "modifiers": [
                    {
                      "productId": "HAL-itm-008-011",
                      "price": 0.29,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    },
                    {
                      "productId": "HAL-itm-008-004",
                      "price": 0,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    },
                    {
                      "productId": "HAL-itm-008-005",
                      "price": 0.29,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    }
                  ]
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-004",
                  "modifiers": [
                    {
                      "productId": "HAL-itm-009-013",
                      "price": 0,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    }
                  ]
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-005",
                  "modifiers": []
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-006",
                  "modifiers": []
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-008",
                  "modifiers": [
                    {
                      "productId": "HAL-itm-013-007",
                      "price": 0,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    }
                  ]
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-010",
                  "modifiers": [
                    {
                      "productId": "HAL-itm-015-001",
                      "price": 0,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    }
                  ]
                }
              ],
              "quantity": 1,
              "note": null
            },
            {
              "lineItemId": 103,
              "description": "Vegetable Fires",
              "price": 1.99,
              "priceType": null,
              "productId": "HAL-itm-002-002",
              "childItems": [],
              "modifierGroups": [
                {
                  "description": null,
                  "productId": "HAL-prg-001-006",
                  "modifiers": []
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-005",
                  "modifiers": []
                }
              ],
              "quantity": 1,
              "note": null
            },
            {
              "lineItemId": 104,
              "description": "Coca-Cola®",
              "price": 1.89,
              "priceType": null,
              "productId": "HAL-itm-003-001",
              "childItems": [],
              "modifierGroups": [
                {
                  "description": null,
                  "productId": "HAL-prg-001-011",
                  "modifiers": [
                    {
                      "productId": "HAL-itm-016-001",
                      "price": 0,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    }
                  ]
                },
                {
                  "description": null,
                  "productId": "HAL-prg-001-012",
                  "modifiers": [
                    {
                      "productId": "HAL-itm-017-001",
                      "price": 0,
                      "priceType": null,
                      "actionCode": null,
                      "quantity": 1
                    }
                  ]
                }
              ],
              "quantity": 1,
              "note": null
            }
          ],
          "modifierGroups": [],
          "quantity": 1,
          "note": null
        }
      ],
      "fulfillment": {
        "fulfillmentType": "TimePickup",
        "asap": true,
        "pickupTime": null,
        "storeLocation": {
          "locationId": "99983"
        }
      },
      "tallyTime": "2018-03-25T12:41:29.949Z",
      "tax": 3.51
    }

请让我知道您对此的看法。提前致谢。

【问题讨论】:

    标签: java json api rest-assured


    【解决方案1】:

    刚刚尝试了以下方法,它对我有用。

    JSONObject 结果 = 新 JSONObject(response.asString());

        JSONArray childItems= results.getJSONArray("items").getJSONObject(0).getJSONArray("childItems");
        
        float Total_Price =0;
        for(int i=0;i<childItems.length();i++)
        {
            //scenario.write(childItems.toString());
            scenario.write(+i +" : "+childItems.getJSONObject(i).getFloat("price"));
            
            Total_Price = Total_Price + childItems.getJSONObject(i).getFloat("price");
        }
        
        Total_Price = Total_Price + response.jsonPath().getFloat("tax");
        
        Assert.assertEquals(Total_Price, response.jsonPath().getFloat("total"),"Total Price in response is not equal to sum of line items price plus tax");
        
    

    【讨论】:

      猜你喜欢
      • 2013-04-20
      • 1970-01-01
      • 1970-01-01
      • 2021-07-12
      • 1970-01-01
      • 1970-01-01
      • 2018-10-05
      • 1970-01-01
      • 2022-05-13
      相关资源
      最近更新 更多