简体   繁体   中英

How to convert JSON to Dictionary in Swift 3 Vapor

I make REST calls from my own server running Vapor to a Firebase server and the response is a JSON object.
How can I get the the elements under "1508321173" without knowing the actual value. This represents a timestamp, so this value is always dynamic in the database.

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)")
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM