amtath 发表于 2023-8-15 23:55:31

api请求之用对象操作构造一个数组对象举例

本帖最后由 amtath 于 2023-8-15 23:57 编辑


有时候,我们调用api做数据对接,传json时会有格式要求。例如:




说明文档:https://open.chanjet.com/docs/fi ... d/t+dj/xhd?id=30787

当我们把这一串复制到http请求命令中之后,对应的表体参数这里直接静态了。orz。咋整,我们的表体可肯定不会永远只有一行


半路出家,卡这个问题好久了。终于有一天,电闪雷鸣,我顿悟了,那是不可能的,查论坛资料,听说可以构造。
于是,叮叮咚咚一翻,捣鼓出来了:结合我们的对象操作和集合操作命令。




执行过程如下:

创建数组对象: array 用时:0毫秒。
    创建变量 array = []
设置变量命令 : entrys 用时:4毫秒。
    SELECT, , , , , FROM 表1
    创建变量 entrys = [{"ID":1,"item":"aa","qty":"100","warehouse":"a","amount":"1000","fbatchno":"11"},{"ID":2,"item":"bb","qty":"200","warehouse":"b","amount":"2000","fbatchno":"22"},{"ID":3,"item":"cc","qty":"50","warehouse":"a","amount":"500","fbatchno":"333"}]
循环命令 用时:1毫秒。
    创建变量 entry = {"ID":1,"item":"aa","qty":"100","warehouse":"a","amount":"1000","fbatchno":"11"}
    创建对象: bankobject 用时:0毫秒。
      创建变量 bankobject = {}
    创建对象: Inventory 用时:0毫秒。
      创建变量 Inventory = {"Code":"aa"}
    创建对象: Warehouse 用时:0毫秒。
      创建变量 Warehouse = {"Code":"a"}
    对象操作(SetProperties):fullobject 用时:0毫秒。
      创建变量 fullobject = {"Inventory":{"Code":"aa"},"Warehouse":{"Code":"a"},"Batchno":"11","Qty":"100","Amount":"1000"}
    追加数组项目到已有数组: array 用时:0毫秒。
      设置变量 array = [{"Inventory":{"Code":"aa"},"Warehouse":{"Code":"a"},"Batchno":"11","Qty":"100","Amount":"1000"}]
    设置变量 entry = {"ID":2,"item":"bb","qty":"200","warehouse":"b","amount":"2000","fbatchno":"22"}
    创建对象: bankobject 用时:0毫秒。
      设置变量 bankobject = {}
    创建对象: Inventory 用时:0毫秒。
      设置变量 Inventory = {"Code":"bb"}
    创建对象: Warehouse 用时:0毫秒。
      设置变量 Warehouse = {"Code":"b"}
    对象操作(SetProperties):fullobject 用时:0毫秒。
      设置变量 fullobject = {"Inventory":{"Code":"bb"},"Warehouse":{"Code":"b"},"Batchno":"22","Qty":"200","Amount":"2000"}
    追加数组项目到已有数组: array 用时:0毫秒。
      设置变量 array = [{"Inventory":{"Code":"aa"},"Warehouse":{"Code":"a"},"Batchno":"11","Qty":"100","Amount":"1000"},{"Inventory":{"Code":"bb"},"Warehouse":{"Code":"b"},"Batchno":"22","Qty":"200","Amount":"2000"}]
    设置变量 entry = {"ID":3,"item":"cc","qty":"50","warehouse":"a","amount":"500","fbatchno":"333"}
    创建对象: bankobject 用时:0毫秒。
      设置变量 bankobject = {}
    创建对象: Inventory 用时:0毫秒。
      设置变量 Inventory = {"Code":"cc"}
    创建对象: Warehouse 用时:0毫秒。
      设置变量 Warehouse = {"Code":"a"}
    对象操作(SetProperties):fullobject 用时:0毫秒。
      设置变量 fullobject = {"Inventory":{"Code":"cc"},"Warehouse":{"Code":"a"},"Batchno":"333","Qty":"50","Amount":"500"}
    追加数组项目到已有数组: array 用时:0毫秒。
      设置变量 array = [{"Inventory":{"Code":"aa"},"Warehouse":{"Code":"a"},"Batchno":"11","Qty":"100","Amount":"1000"},{"Inventory":{"Code":"bb"},"Warehouse":{"Code":"b"},"Batchno":"22","Qty":"200","Amount":"2000"},{"Inventory":{"Code":"cc"},"Warehouse":{"Code":"a"},"Batchno":"333","Qty":"50","Amount":"500"}]
创建对象: Dto 用时:0毫秒。
    创建变量 Dto = {"Dto":[{"Inventory":{"Code":"aa"},"Warehouse":{"Code":"a"},"Batchno":"11","Qty":"100","Amount":"1000"},{"Inventory":{"Code":"bb"},"Warehouse":{"Code":"b"},"Batchno":"22","Qty":"200","Amount":"2000"},{"Inventory":{"Code":"cc"},"Warehouse":{"Code":"a"},"Batchno":"333","Qty":"50","Amount":"500"}]}
返回命令 用时:0毫秒。
错误码: 0
信息: {"Dto":[{"Inventory":{"Code":"aa"},"Warehouse":{"Code":"a"},"Batchno":"11","Qty":"100","Amount":"1000"},{"Inventory":{"Code":"bb"},"Warehouse":{"Code":"b"},"Batchno":"22","Qty":"200","Amount":"2000"},{"Inventory":{"Code":"cc"},"Warehouse":{"Code":"a"},"Batchno":"333","Qty":"50","Amount":"500"}]}
'服务端命令1'命令执行结束, 用时:22毫秒。最后,再调用http请求的时候,把静态的参数,改成我们构造好的数组对象传进去。


附上工程,方便跟我一样想不开的格友参考。




赛龙周 发表于 2023-8-18 09:45:10

对象和集合操作是个好东西。
如果能把json解析结果copy出来就完美了:lol

wwwlliu 发表于 2023-11-11 14:30:49

留下来,学习下!~~

true 发表于 2023-11-11 16:54:12

太抽象了 始终感觉活字格这种比用代码还抽象

olookok 发表于 2024-11-6 18:54:57

正瞌睡,找到你这个枕头 。:D:D;P;P

amtath 发表于 2024-11-6 19:21:47

olookok 发表于 2024-11-6 18:54
正瞌睡,找到你这个枕头 。

受益于社区,反哺社区:itwn:

ZDYW 发表于 2024-11-15 10:08:29

学习                                             
页: [1]
查看完整版本: api请求之用对象操作构造一个数组对象举例