2016-12-13 1 views
0

ich nicht in der Lage die von diesem JSON DriverID bekommen, wie Daten von JSON-Array analysieren

[data: [{"id":"619","driverId":"6789","starting_time":"2016-12-12 23:24:50","end_time":null}]] 

Hier ist meine vollständige Code

//selector Method SESSION_STATUS_REQUEST_COMPLETE 
    func afterServiceStatus(notification : NSNotification) { 

     print(notification.userInfo!) 
     guard let data = notification.userInfo!["data"] else{ 
     print("data have no idea ") 
     return 
     } 

     if let driverId = notification.userInfo![0]!["driverId"] { 

      print(driverId) 

     } 
    //  if let driverId = data["driverId"] as? String{ 
    // 
    //   print(driverId) 
    //  } 


    } 
+0

Ihre Bedingung Änderung dieser eine, wenn DriverID = Daten lassen! [0]! [ "DriverID"] { print (DriverID) } –

+0

Sie einen Blick auf swiftyJSON ergreifen sollte, um dieses "loswerden! 'Wahnsinn – HaneTV

+0

@SapanaRanipa Force zu gelöschten Daten nach'! ' dann Laufzeitfehler schlechter Befehlsfehler trowing –

Antwort

3

du ...

func afterServiceStatus(notification : NSNotification) { 

      print(notification.userInfo!) 
      guard let data = notification.userInfo!["data"] else{ 
       print("data have no idea ") 
       return 
      } 

      let driverId = notification.userInfo![0]?.objectForKey("data")?.valueForKey("driverId") 

       print(driverId) 



     } 
versuchen
0

Sie sollten versuchen, die ganze Kraft auspackt zu vermeiden, weil sie Ihren Code anfällig für Abstürze machen, wenn Sie JSON in einem unerwarteten Format zurückbekommen.

Dies sollte die driverId aus dem Wörterbuch abrufen:

func afterServiceStatus(notification: NSNotification) { 

    guard 
    let userInfo = notification.userInfo as? [String: Any], 
    let data = userInfo["data"] as? [[String: Any]], 
    let driverId = data[0]["driverId"] as? String 
    else { 
    return 
    } 

    print(driverId) 
} 
+0

'[NSObject: AnyObject]?' ist nicht in '[String: Any]' konvertierbar –

+0

Definition Konflikte mit dem vorherigen Wert –

2
func afterServiceStatus(notification : NSNotification) { 

      print(notification.userInfo!) 
      guard let data = notification.userInfo!["data"] else{ 
       print("data have no idea ") 
       return 
      } 
if let driverId = notification.userInfo[0]["data"]["driverId"].string 
     { 
      print(driverId) 
     } 
} 

Wenn nicht, dann Link weiterleiten mich arbeiten, ich Ich gebe dir die richtige Lösung.