如何将 JSON 转换为 Swift Dictionary 以进行 HTTP POST
Posted
技术标签:
【中文标题】如何将 JSON 转换为 Swift Dictionary 以进行 HTTP POST【英文标题】:How can I convert JSON to Swift Dictinary inorder to make an HTTP POST 【发布时间】:2020-08-28 09:03:32 【问题描述】:我无法成功地将我的 json 数据构建到一个 swift 字典中,以便通过 API 发送我的 HTTP 正文
这就是我的 Json 的结构:
"idNo": "string",
"name": "string",
"isRegistered": true,
"customMessage": "string",
"riskStatus": "Low",
"exposureBasedRiskStatus": "Low",
"symptomBasedRiskStatus": "Low",
"riskMessage": "string",
"doYouHaveATemperature": true,
"temperature": 0,
"asessmentQuestion": [
"question": "string",
"answer": "string",
"createdBy": "string"
,
"question": "string",
"answer": "string",
"createdBy": "string"
,
"question": "string",
"answer": "string",
"createdBy": "string"
,
"question": "string",
"answer": "string",
"createdBy": "string"
,
"question": "string",
"answer": "string",
"createdBy": "string"
]
对象“assessmentQuestion”是一系列不同的问题,似乎无法弄清楚如何将此结构转换为快速字典或其他推荐格式,以便我能够发布我的数据。我的 API 总是说错误的请求,我很确定我没有正确映射 json 数据。
这是我如何尝试映射我的 json 数据的 sn-p:
var dictionary = [String:Any]()
dictionary["1. How old are you?"] = model.riskExposureBasedAssessment[0].answer
dictionary["2. Have you ever visited a COVID affected country?"] = model.riskExposureBasedAssessment[1].answer
dictionary["3. do you frequently experience flu like symptoms?"] = model.riskExposureBasedAssessment[2].answer
dictionary["4. Where you providing care in a non-health setting for a person with symptomatic COVID-19 infection"] = model.riskExposureBasedAssessment[3].answer
dictionary["5. Did you come in close contact* with a person with symptomatic laboratory-confirmed COVID-19 infection?"] = model.riskExposureBasedAssessment[4].answer
let parameters = [
"idNo": model.id,
"name": model.name,
"isRegistered": model.isRegistered,
"customMessage": model.customResponse,
"riskStatus": model.riskStatus,
"exposureBasedRiskStatus": model.exposureBasedRiskStatus,
"symptomBasedRiskStatus": model.symptomBasedRiskStatus,
"riskMessage": model.riskMessage,
"doYouHaveATemperature": model.doYouHaveATemperature,
"temperature": model.temperature,
"exposureBasedAssessments": dictionary
] as [String:Any]
let postData = try? JSONSerialization.data(withJSONObject: parameters)
【问题讨论】:
您是否尝试在 Postman 或 Paw 中重现此内容?如果没有实际的 API 文档,我们无法判断这里出了什么问题。 你能做到吗model.riskExposureBasedAssessment[0].question
?那会为你节省很多。你不使用 Codable 吗?
是的,我确实在邮递员中对其进行了测试,效果很好
【参考方案1】:
这是一个字典数组,所以:
var dictionaries: [[String: Any]] = []
// Populate dictionaries
// ...
let parameters = [
"idNo": model.id,
"name": model.name,
"isRegistered": model.isRegistered,
"customMessage": model.customResponse,
"riskStatus": model.riskStatus,
"exposureBasedRiskStatus": model.exposureBasedRiskStatus,
"symptomBasedRiskStatus": model.symptomBasedRiskStatus,
"riskMessage": model.riskMessage,
"doYouHaveATemperature": model.doYouHaveATemperature,
"temperature": model.temperature,
"exposureBasedAssessments": dictionaries
] as [String:Any]
然后,填充它:
var dict1: [String: Any] = [:]
dict1["question"] = "1. How old are you?"
dict1["answer"] = model.riskExposureBasedAssessment[0].answer
dictionaries.append(dict1)
var dict2: [String: Any] = [:]
dict2["question"] = "2. Have you ever visited a COVID affected country?"
dict2["answer"] = model.riskExposureBasedAssessment[1].answer
dictionaries.append(dict2)
...
或
var dict1: [String: Any] = ["question": "1. How old are you?",
"answer": model.riskExposureBasedAssessment[0].answer]
dictionaries.append(dict1)
var dict2: [String: Any] = ["question": "2. Have you ever visited a COVID affected country?"",
"answer": model.riskExposureBasedAssessment[1].answer]
dictionaries.append(dict2)
...
但是,我想你可以从model.riskExposureBasedAssessment[n]
检索问题,
而不是"1. How old are you?"
,你不能用model.riskExposureBasedAssessment[0].question
吗?
如果是这种情况,您可以使用 for 循环: 所以我会选择:
for aQuestionModel in model.riskExposureBasedAssessment
let questionDict = ["question": aQuestionModel.question,
"answer": aQuestionModel.answer]
dictionaries.append(questionDict)
或者,一旦你掌握了基本算法、闭包和map()
:
var dictionaries = model.riskExposureBasedAssessment.map ["question": $0.question, "answer": $0.answer]
它缺少“createdBy”,我不知道在哪里可以找到它以及它是否是可选的,但我认为您应该能够在需要时添加它。
NotaBene: 代码未针对编译器进行测试。它应该可以工作,可能有一两个错别字。
【讨论】:
感谢您提供简单直接的方法,我让我的生活变得复杂,因此让自己感到困惑。以上是关于如何将 JSON 转换为 Swift Dictionary 以进行 HTTP POST的主要内容,如果未能解决你的问题,请参考以下文章
如何将 JSON 转换为 Swift Dictionary 以进行 HTTP POST
Swift:如何将带有 Alamofilre 或 SwiftyJSON 的 JSON 字符串转换为 ObjectMapper?