gpt4 book ai didi

arrays - 如何在 Swift 3 Vapor 中将 JSON 转换为字典

转载 作者:行者123 更新时间:2023-11-30 12:04:38 25 4
gpt4 key购买 nike

我从自己运行 Vapor 的服务器向 Firebase 服务器发出 REST 调用,并且响应是一个 JSON 对象。
在不知道实际值的情况下如何获取“1508321173”下的元素。这代表一个时间戳,因此该值在数据库中始终是动态的。

let jsonObject:JSON = [
"EmailAddress": "damiena@gmail.com",
"PhoneNumber": 07672395038,
"RescheduledByCustomer":
[1508321173:
["BookingStatusClient": "true",
"DateAndTime": "Fri, 20 Oct 2017 18:30"]
],
"BookingNumber":"726070304"
.
.
.
]



let jsonObject:JSON?

if let arrayOfDict:Any = jsonObject?.object?["RescheduledByCustomer"]{
print("arrayOfDict is \(arrayOfDict)")

prints: arrayOfDict is JSON(node: Node.Node.object(["1508321173": Node.Node.object(["BookingStatusClient": Node.Node.string("true"),
"DateAndTime": Node.Node.string("Fri, 20 Oct 2017 18:30")])]))

    if let underTimeStamp = arrayOfDict as? [[String: Any]]{
print("underTstamp is \(underTstamp)")
//it does not print anything
//how can I get the elements under "1508321173" ?
}
}

最佳答案

//get the object under timeStamp 1508321173


let arrayOfTimeStamps = jsonObject?["RescheduledByCustomer"]?.object?.keys
for timeStampKey in arrayOfTimeStamps?.array ?? [] {
let dictUnderTstamp = jsonObject?["RescheduledByCustomer"]?.object?[timeStampKey]


for i in (dictUnderTstamp?.object)!{
print("i key dictUnderTstamp is \(i.key)")
print("i value dictUnderTstamp is \(i.value)")
}
}

关于arrays - 如何在 Swift 3 Vapor 中将 JSON 转换为字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46812062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com