【问题标题】:Dynamically populate and parse JSON in REST API在 REST API 中动态填充和解析 JSON
【发布时间】:2020-01-21 14:14:06
【问题描述】:

如何动态填充 JSON 请求并将每个条形码、serial_number 值作为具有barcode valueserial_number value 的动态内容数据的自动化发送到 REST API哪个来自 Oracle DB 表?

barcode value: ["324433435343413","23432444354334433","88634354334434",....]
serial_number value: ["2999","2332","7876",....]

发送到 REST API 的 JSON 有效负载请求:

{
  "selectType": "library",
  "num": "0",
  "size": "15",
  "title": "RISE",
  "contentField": [
    {
      "key": "barcode",
      "value": {value}  
    },
    {
      "key": "serial_number",
      "value": {value} 
    }
  ]
}

从 Oracle DB/另一个 JSON 文件中动态读取这两个值作为测试数据,并准备上述 JSON Payload 请求,并且必须发送到 Rest API。有 5000 多个产品条形码,存在 serial_num 值必须从数据库中获取/读取,并为每个值准备 Payload 并发送到 API。

【问题讨论】:

  • 创建 JSON 的类请求(Json.NET 将为您制作类 -> JSON 转换)然后用所需的数组填充 ContentFileld 似乎是个好主意...问题可能需要首先澄清

标签: c# json rest automation restsharp


【解决方案1】:

您可以尝试使用 foreach 为每个 contentField 动态生成

【讨论】:

    【解决方案2】:

    我会像这样构建一个 JsonRequest 模型类:

    class JsonRequest
    {
        public string SelectType { get; set; }
        public string Num { get; set; }
        public string Size { get; set; }
        public string Title { get; set; }
        // <serialNumber, Barcode> Assuming this data is linked and SerialNumber is unquie.
        public Dictionary<string, string> ContentField { get; set; }
    }
    

    您可能希望将 ContentField 更改为 ContentField 列表。如果字典没有给你想要的。

    class ContentField
    {
        public string SerialNumber { get; set; }
        public string Barcode { get; set; }
    }
    

    然后使用:

    JsonConvert.SerializeObject(jsonRequest);
    

    序列化为 Json 字符串并发送。

    【讨论】:

    • 如何动态地构建带有动态条形码、序列号值的 Json 字符串并解析以发送到 API。考虑数据集有 5K 或 6K 值来处理每次运行。
    • 请提供完整样本
    猜你喜欢
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多