英文:
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
- 从 response.body 转换为 JSON 字符串
 - 转换为字典
 - 判断字典并手动创建结构对象
 
英文:
I solve it by:
- convert to json string from response.body
 - convert to dict
 - judge the dict, and make a struct object by manual.
 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论