Jmeter beanshell 获取购物车的所有商品

2019-08-07  本文已影响0人  Canon_2020

上一个接口返回的数据

{
    "code": 0,
    "msg": "success",
    "data": {
        "shopId": 1,
        "shopName": "测试店铺修改",
        "skuAmount": null,
        "totalAmount": null,
        "promotionAmount": null,
        "couponAmount": null,
        "payAmount": null,
        "freightAmount": null,
        "offsetPrice": null,
        "cartItemList": [
            {
                "cartPromotion": {
                    "promotionId": null,
                    "couponCode": null,
                    "promotionName": null,
                    "promotionType": null,
                    "promotionTypeName": null,
                    "promotionTag": null,
                    "conditionId": null,
                    "prmDisPrice": null,
                    "prmDiscountRate": null,
                    "freeShipping": null,
                    "description": null,
                    "productItemList": [
                        {
                            "cartId": 2175590987728433,
                            "productId": 1919550002039029,
                            "productCode": "",
                            "skuId": 2004453183982352,
                            "skuCode": "6970582790108",
                            "skuName": "无痕遮瑕液",
                            "skuSpec": "粉紫|100ml|100片",
                            "originPrice": 100.00,
                            "price": 0.30,
                            "payPrice": 0.30,
                            "skuQuantity": 1,
                            "skuPromotionQuantity": 1,
                            "skuImage": "https://pub-img.perfectdiary.com/image/2019/07/1f919825c8574cf496d172c374f4c4ea.jpg",
                            "limitNum": 0,
                            "skuStockNum": 98,
                            "isSoldOut": 0,
                            "orderLimit": null,
                            "isEnjoyDiscount": null,
                            "isValid": 0,
                            "stockReduce": 1,
                            "freightType": 1,
                            "freight": 1.56,
                            "templateId": 0,
                            "deliveryType": 1,
                            "prmDisPrice": null,
                            "prmDiscountRate": null,
                            "promotionId": null,
                            "promotionName": null,
                            "promotionType": null,
                            "promotionTypeName": null,
                            "promotionTip": null,
                            "isSelfSelling": 1,
                            "detailType": 1,
                            "supplierId": 0,
                            "limitTimeProduct": null,
                            "prmContentId": null,
                            "prmContentName": null,
                            "promotionTag": null
                        }
                    ],
                    "promotionOrderGifts": null
                },
                "cartProductItems": [
                    {
                        "cartId": 2175590987728433,
                        "productId": 1919550002039029,
                        "productCode": "",
                        "skuId": 2004453183982352,
                        "skuCode": "6970582790108",
                        "skuName": "无痕遮瑕液",
                        "skuSpec": "粉紫|100ml|100片",
                        "originPrice": 100.00,
                        "price": 0.30,
                        "payPrice": 0.30,
                        "skuQuantity": 1,
                        "skuPromotionQuantity": 1,
                        "skuImage": "https://pub-img.perfectdiary.com/image/2019/07/1f919825c8574cf496d172c374f4c4ea.jpg",
                        "limitNum": 0,
                        "skuStockNum": 98,
                        "isSoldOut": 0,
                        "orderLimit": null,
                        "isEnjoyDiscount": null,
                        "isValid": 0,
                        "stockReduce": 1,
                        "freightType": 1,
                        "freight": 1.56,
                        "templateId": 0,
                        "deliveryType": 1,
                        "prmDisPrice": null,
                        "prmDiscountRate": null,
                        "promotionId": null,
                        "promotionName": null,
                        "promotionType": null,
                        "promotionTypeName": null,
                        "promotionTip": null,
                        "isSelfSelling": 1,
                        "detailType": 1,
                        "supplierId": 0,
                        "limitTimeProduct": null,
                        "prmContentId": null,
                        "prmContentName": null,
                        "promotionTag": null
                    }
                ]
            },
            {
                "cartPromotion": {
                    "promotionId": 2004453452417915,
                    "couponCode": null,
                    "promotionName": "满减活动5-1",
                    "promotionType": 1010,
                    "promotionTypeName": "满N元减送",
                    "promotionTag": "满N元减送",
                    "conditionId": null,
                    "prmDisPrice": null,
                    "prmDiscountRate": null,
                    "freeShipping": null,
                    "description": null,
                    "productItemList": [
                        {
                            "cartId": 2175590182424510,
                            "productId": 1919550002038056,
                            "productCode": "",
                            "skuId": 1919550002038065,
                            "skuCode": "6970582790108",
                            "skuName": "轻薄粉底液",
                            "skuSpec": "完美日记 轻薄无瑕粉底液C10",
                            "originPrice": 50.00,
                            "price": 15.00,
                            "payPrice": 126.00,
                            "skuQuantity": 9,
                            "skuPromotionQuantity": 9,
                            "skuImage": "https://pub-img.perfectdiary.com/image/2019/07/dfa5fda6d1f94d8fa95a0b9346c25fcb.jpg",
                            "limitNum": 0,
                            "skuStockNum": 47,
                            "isSoldOut": 0,
                            "orderLimit": null,
                            "isEnjoyDiscount": null,
                            "isValid": 0,
                            "stockReduce": 1,
                            "freightType": 1,
                            "freight": 1.56,
                            "templateId": 0,
                            "deliveryType": 1,
                            "prmDisPrice": 1.00,
                            "prmDiscountRate": null,
                            "promotionId": 2175589913986512,
                            "promotionName": "前一小时第二件1元",
                            "promotionType": 1000,
                            "promotionTypeName": "限时折扣",
                            "promotionTip": null,
                            "isSelfSelling": 1,
                            "detailType": 1,
                            "supplierId": 0,
                            "limitTimeProduct": true,
                            "prmContentId": null,
                            "prmContentName": null,
                            "promotionTag": "限时折扣"
                        }
                    ],
                    "promotionOrderGifts": null
                },
                "cartProductItems": [
                    {
                        "cartId": 2175590182424510,
                        "productId": 1919550002038056,
                        "productCode": "",
                        "skuId": 1919550002038065,
                        "skuCode": "6970582790108",
                        "skuName": "轻薄粉底液",
                        "skuSpec": "完美日记 轻薄无瑕粉底液C10",
                        "originPrice": 50.00,
                        "price": 15.00,
                        "payPrice": 126.00,
                        "skuQuantity": 9,
                        "skuPromotionQuantity": 9,
                        "skuImage": "https://pub-img.perfectdiary.com/image/2019/07/dfa5fda6d1f94d8fa95a0b9346c25fcb.jpg",
                        "limitNum": 0,
                        "skuStockNum": 47,
                        "isSoldOut": 0,
                        "orderLimit": null,
                        "isEnjoyDiscount": null,
                        "isValid": 0,
                        "stockReduce": 1,
                        "freightType": 1,
                        "freight": 1.56,
                        "templateId": 0,
                        "deliveryType": 1,
                        "prmDisPrice": 1.00,
                        "prmDiscountRate": null,
                        "promotionId": 2175589913986512,
                        "promotionName": "前一小时第二件1元",
                        "promotionType": 1000,
                        "promotionTypeName": "限时折扣",
                        "promotionTip": null,
                        "isSelfSelling": 1,
                        "detailType": 1,
                        "supplierId": 0,
                        "limitTimeProduct": true,
                        "prmContentId": null,
                        "prmContentName": null,
                        "promotionTag": "限时折扣"
                    }
                ]
            }
        ],
        "customerOrderAccountVO": null,
        "coupon": null,
        "orderAddressVO": null,
        "promotionProductList": null
    }
}

下一个接口传入的参数

{
    "accountId": 0,
    // cartProducts 通过 beanshell 处理后的传入参数
    "cartProducts": [
        {
            "cartId": 2175590182424566,
            "detailType": 1,
            "productId": 1919550002038056,
            "quantity": 2,
            "skuId": 1919550002038065
        }
    ]
}

beanshell 脚本处理

// 方法一
import java.util.ArrayList;
import java.util.HashMap;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

String response_data = prev.getResponseDataAsString(); // 获取接口返回数据
JSONObject object = JSONObject.parseObject(response_data);
JSONObject object1 = JSONObject.parseObject(object.getString("data"));
JSONArray array = JSONArray.parseArray(object1.getString("cartItemList"));
String res_str = "[";
// 获取购物车的多个商品
for (Object object2 : array) {
    JSONObject object3 = JSONObject.parseObject(JSON.toJSONString(object2));
    JSONArray array3 = JSONArray.parseArray(object3.getString("cartProductItems"));
    // 获取单个商品数据
    for (Object object4 : array3) {
        JSONObject object5 = JSONObject.parseObject(JSON.toJSONString(object4));
        res_str += "{\"cartId\":" + String.valueOf(object5.get("cartId")) + ",\"detailType\":"
                + String.valueOf(object5.get("detailType")) + ",\"productId\":"
                + String.valueOf(object5.get("productId")) + ",\"quantity\":"
                + String.valueOf(object5.get("skuQuantity")) + ",\"skuId\":" + String.valueOf(object5.get("skuId"))
                + "},";
    }
}
res_str = res_str.substring(0, res_str.length()-1) + "]";
log.info(res_str);

// 方法二
import java.util.ArrayList;
import java.util.HashMap;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

String response_data = prev.getResponseDataAsString(); // 获取接口返回数据
JSONObject object = JSONObject.parseObject(response_data);
JSONObject object1 = JSONObject.parseObject(object.getString("data"));
JSONArray array = JSONArray.parseArray(object1.getString("cartItemList"));
String res_str = "[";

var i=0;
for (Object object2 : array) {
   JSONObject object3 = JSONObject.parseObject(JSON.toJSONString(object2));
   JSONArray array3 = JSONArray.parseArray(object3.getString("cartProductItems"));
   
   for (Object object4 : array3) {
    JSONObject object5 = JSONObject.parseObject(JSON.toJSONString(object4));
    if(i==0){
        res_str += "{\"cartId\":" + String.valueOf(object5.get("cartId")) + ",\"detailType\":"
            + String.valueOf(object5.get("detailType")) + ",\"productId\":"
            + String.valueOf(object5.get("productId")) + ",\"quantity\":"
            + String.valueOf(object5.get("skuQuantity")) + ",\"skuId\":" + String.valueOf(object5.get("skuId"))
            + "}";
    }else{
        res_str += ",{\"cartId\":" + String.valueOf(object5.get("cartId")) + ",\"detailType\":"
            + String.valueOf(object5.get("detailType")) + ",\"productId\":"
            + String.valueOf(object5.get("productId")) + ",\"quantity\":"
            + String.valueOf(object5.get("skuQuantity")) + ",\"skuId\":" + String.valueOf(object5.get("skuId"))
            + "}";
    }
  }
  i=i+1;
}

res_str += "]";
log.info(res_str);


上一篇下一篇

猜你喜欢

热点阅读