本文主要是介绍基于python可伸缩JSON格式列表实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
对于消息体为一个json格式列表,列表长度变化的代码设计,如下实现可供参考。
1、python语言实现(直接取值)
#coding=utf-8n = 2 # 行项目数
productCode = ['11111','222222','333333']
unit = ['H06','H07','H08']
qty = [6,7,8]
items = []for i in range(0, n):item = {"itemType": "${SOZKitemType}","promotionFlg": None,"deliveryTime": "${currentTime}","excessRatio": 0,"storageLocation": "${ZKstorageLocation}","itemNo": (i+1)*10,"merchantProductCode": productCode[i],"customerSupplierCode": None,"avaiableQtyUnit": unit[i],"referenceItemNo": (i+1)*10,"orderQty": qty[i],"canExcess": "0","orderUnit": unit[i],"avaiableQty": qty[i],"orderNo": "20${orderNo}","stockType": "${stockType}","batch": "","logisticsType": "ZK","subProducts": None}items.append(item)
print(items)
2、python语言在Jmeter中实现(直接取值)
import json
n = 2 # 行项目数
items = []for i in range(0, n):item = {"itemType": "${SOZKitemType}","promotionFlg": None,"deliveryTime": "${currentTime}","excessRatio": 0,"storageLocation": "${ZKstorageLocation}","itemNo": (i+1)*10,"merchantProductCode": str(vars.get('merchantProductCode'+str(i+1))),"customerSupplierCode": None,"avaiableQtyUnit": str(vars.get('SUF'+str(i+1))),"referenceItemNo": (i+1)*10,"orderQty": str(vars.get('SOZKorderQty'+str(i+1))),"canExcess": "0","orderUnit": str(vars.get('SUF'+str(i+1))),"avaiableQty": str(vars.get('SOZKorderQty'+str(i+1))),"orderNo": "20${orderNo}","stockType": "${stockType}","batch": "","logisticsType": "ZK","subProducts": None}items.append(item)
data = json.dumps(items,indent=4)
vars.put('newItems',data)
3、python语言在Jmeter中实现(不直接取值、纯JSON消息体制作),这种方式的好处是先将消息体都开发完整了,不用执行Jmeter脚本时候去拼接消息体,执行更快。
import jsonn=2000 #行项目数
totalItem=[]
allParas = {"orderType": "${SOZKorderType}","CreatedTime": "${currentTime}","deliveryTime": "","city": "","customerCode": "${customerCode}","vendorId": "","reference2": "20${orderNo}","distributionModeCode": "${distributionModeCode}","cpoOrderNo": "","telPhone": "","province": "","epCode": "${epCode}","shippingConditionCode": "${shippingConditionCode}","traceId": "${__UUID}","creator": "${creator}","merchantCode": "${outMerchantCode}","address": "","orderNo": "20${orderNo}","countryNo": "","updateIndicator": "","customerDesc": "","route": "111","whCode": "${whCode}","district": "","deliveryPriorityCode": "${deliveryPriorityCode}","contacts": "","items": ""}for i in range(0,n):item = {"itemType": "${SOZKitemType}","promotionFlg": "","deliveryTime": "${currentTime}","excessRatio": 0,"storageLocation": "${ZKstorageLocation}","itemNo": str((i + 1) * 10),"merchantProductCode": '${' + 'merchantProductCode' + str(i + 1) + '}',"customerSupplierCode": "","avaiableQtyUnit": '${' + 'SUF' + str(i + 1) + '}',"referenceItemNo": str((i + 1) * 10),"orderQty": '${' + 'SOZKorderQty' + str(i + 1) + '}',"canExcess": "0","orderUnit": '${' + 'SUF' + str(i + 1) + '}',"avaiableQty": '${' + 'SOZKorderQty' + str(i + 1) + '}',"orderNo": "20${orderNo}","stockType": "${stockType}","batch": "","logisticsType": "ZK","subProducts": ""}item['promotionFlg'] = None #当我们从外部数据源获取数据时,经常会遇到一些空值或者null,在Python中,null或者空可以用 None 表示。item['customerSupplierCode'] = Noneitem['subProducts'] = NonetotalItem.append(item)
allParas['items'] = totalItem
allParas['deliveryTime'] = None
allParas['city'] = None
allParas['vendorId'] = None
allParas['city'] = None
allParas['cpoOrderNo'] = None
allParas['telPhone'] = None
allParas['province'] = None
allParas['address'] = None
allParas['countryNo'] = None
allParas['updateIndicator'] = None
allParas['customerDesc'] = None
allParas['district'] = None
allParas['contacts'] = Nonewith open(r'D:\apache-jmeter-5.6.3\bin\TestData\common\requestBody.txt','w+') as file:json.dump(allParas,file,indent=4)
这篇关于基于python可伸缩JSON格式列表实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!