如何解决这个问题:Vapor接收响应属性输出格式已更改?

huangapple go评论39阅读模式
英文:

how to solve this: vapor receive response property output format is changed?

问题

struct ReplicateModelData: Codable {
  let output: String? // Change to [OutputFile] for flexibility
}

struct OutputFile: Codable {
  let file: String
}

try response.content.decode(ReplicateModelData.self)
英文:

I want use a content struct to decode the response, but some case the output property is:

"output": [
    {
        "file": "aaa.jpg"
    }
],

and some case output property is:

"output": "aaa.jpg"

how can I do for this?

struct ReplicateModelData: Codable {
  let output: String?
}

try response.content.decode(ReplicateModelData.self)

output type is changed, and cannot decode success everytime.

答案1

得分: 1

  1. 从 response.body 转换为 JSON 字符串
  2. 转换为字典
  3. 判断字典并手动创建结构对象
英文:

I solve it by:

  1. convert to json string from response.body
  2. convert to dict
  3. judge the dict, and make a struct object by manual.

huangapple
  • 本文由 发表于 2023年5月10日 11:38:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76214727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定