【问题标题】:How to convert JSON to Dictionary in Swift 3 Vapor如何在 Swift 3 Vapor 中将 JSON 转换为字典
【发布时间】:2018-03-30 10:32:04
【问题描述】:

我从我自己的运行 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)") 

打印: arrayOfDict 是 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" ?
                }          
    }

【问题讨论】:

    标签: arrays json swift vapor


    【解决方案1】:
    //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)")
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-19
      • 2017-03-05
      • 1970-01-01
      相关资源
      最近更新 更多