【发布时间】:2020-03-30 22:15:09
【问题描述】:
我尝试为 JSON 编码准备数据:
var i = 0
for (data) in students {
var variableNewData = ["\(students[i].id)":["timestampValue":"\(students[i].timestampValue)"]]
variableNewData["updateTime"] = ["updateTime":"\(students[i].timestampValue)"]
variableNewData["createTime"] = ["createTime":"\(students[i].timestampValue)"]
i += 1
let finalParameter = ["class":variableNewData]
print("LastParameter:",finalParameter)}
}
我需要这种格式的数据:
{"class": {"studentOne": {"timestampValue": "2020-02-04" },"studentTwo ":{ "timestampValue": "2020-02-05" }},"createTime": "2020-03-30","updateTime": "2020-03-30"}
但我明白了:class、id、timestampValue 似乎没问题,但创建和更新时间是错误的。 感谢您的任何建议。
{"class":{"createTime":{"createTime":"2020-03-30"},"studentOne":{"timestampValue":"2020-02-04"},"updateTime":{"createTime":"2020-03-30"}}}
【问题讨论】:
-
顺便说一句,您是否喜欢这种结构,其中此
class字典中的键由学生ID 键控?在class内的键中包含唯一标识符会使创建和使用此 JSON 变得更加困难。一个简单的字典数组更容易处理...