簡體   English   中英

如何在Swift 3 Vapor中將JSON轉換為Dictionary

[英]How to convert JSON to Dictionary in Swift 3 Vapor

我從自己的運行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.object([“ 1508321173”:Node.Node.object([“ BookingStatusClient”:Node.Node.string(“ true”),
“ DateAndTime”:Node.Node.string(“星期五,2017年10月20日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)")
            }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM