如何根据D365获取的箱子数量重复JSON payload指定部分?
实现根据箱子数量重复JSON数组对象的方案
核心思路
从D365系统获取箱子数量N,基于预设的基础装箱条目对象,重复生成N个独立实例并填充到radSrvcItemDTOList数组中,最终组装成完整的JSON payload发送给第三方服务。
C#实现(适用于D365插件/自定义服务)
1. 定义JSON对应的数据模型
// 装箱条目实体类 public class RadSrvcItemDTO { public string srvcId { get; set; } public string weight { get; set; } public string volL { get; set; } public string volW { get; set; } public string volH { get; set; } public string cont { get; set; } public string qunt { get; set; } } // 完整账单请求实体类 public class BillingPayload { // 可添加其他必填字段 public List<RadSrvcItemDTO> radSrvcItemDTOList { get; set; } }
2. 从D365获取箱子数量
// 示例:从D365装箱单实体读取数量 int boxCount = 0; var packingSlip = service.Retrieve("packingslip", new Guid("装箱单ID"), new ColumnSet("boxcount")); if (packingSlip.Contains("boxcount")) { boxCount = (int)packingSlip["boxcount"]; }
3. 生成重复的装箱条目数组
// 创建基础装箱条目模板 var baseBoxItem = new RadSrvcItemDTO { srvcId = "", weight = "1.000", volL = "22", volW = "17", volH = "27", cont = "", qunt = "1" }; // 生成N个独立条目(避免引用复用) var itemList = new List<RadSrvcItemDTO>(); for (int i = 0; i < boxCount; i++) { itemList.Add(new RadSrvcItemDTO { srvcId = baseBoxItem.srvcId, weight = baseBoxItem.weight, volL = baseBoxItem.volL, volW = baseBoxItem.volW, volH = baseBoxItem.volH, cont = baseBoxItem.cont, qunt = baseBoxItem.qunt }); } // 组装完整请求payload var billingPayload = new BillingPayload { radSrvcItemDTOList = itemList // 设置其他必填字段 };
4. 序列化并发送请求
using System.Text.Json; using System.Net.Http; // 序列化为JSON字符串 var jsonPayload = JsonSerializer.Serialize(billingPayload); // 发送POST请求到第三方服务 var httpClient = new HttpClient(); var response = await httpClient.PostAsync("第三方服务接口URL", new StringContent(jsonPayload, System.Text.Encoding.UTF8, "application/json")); response.EnsureSuccessStatusCode();
X++实现(适用于D365原生开发)
public void generateBillingPayload() { int boxCount; // 从D365仓储发货表读取箱子数量 WHSShippingTable shippingTable = WHSShippingTable::find("你的装箱单ID"); if (shippingTable) { boxCount = shippingTable.NumOfBoxes; // 假设对应字段为NumOfBoxes } // 创建基础装箱条目JSON对象 JsonObject baseItem = new JsonObject(); baseItem.add("srvcId", ""); baseItem.add("weight", "1.000"); baseItem.add("volL", "22"); baseItem.add("volW", "17"); baseItem.add("volH", "27"); baseItem.add("cont", ""); baseItem.add("qunt", "1"); // 生成重复条目数组 JsonArray itemArray = new JsonArray(); for (int i = 1; i <= boxCount; i++) { itemArray.add(baseItem.copy()); // 复制对象避免引用复用 } // 组装完整请求JSON JsonObject payload = new JsonObject(); payload.add("radSrvcItemDTOList", itemArray); // 添加其他必填字段 // 转换为字符串并发送请求(此处省略HTTP调用逻辑) str jsonStr = payload.toString(); }
内容的提问来源于stack exchange,提问作者Dishanth Varadaraj
相关产品推荐
相关产品推荐

