2016-09-15 6 views
0

Ich möchte diesen JSON analysieren, auf der obersten Ebene eingehende JSON ist ein Array, wie kann auf Informationen des Wörterbuchs in Array zugreifen?JSON Struktur zum Analysieren von JSON-Daten

[ 
     { 
     "id": 1, 
     "name": "Leanne Graham", 
     "username": "Bret", 
     "email": "[email protected]", 
     "address": { 
      "street": "Kulas Light", 
      "suite": "Apt. 556", 
      "city": "Gwenborough", 
      "zipcode": "92998-3874", 
      "geo": { 
      "lat": "-37.3159", 
      "lng": "81.1496" 
      } 
     }, 
     "phone": "1-770-736-8031 x56442", 
     "website": "hildegard.org", 
     "company": { 
      "name": "Romaguera-Crona", 
      "catchPhrase": "Multi-layered client-server neural-net", 
      "bs": "harness real-time e-markets" 
     } 
     } 
] 

das ist mein Code, aber ich weiß nicht, wie Wörterbuchdaten erkennen.

func profileFromJSONData(data : NSData) -> ProfileResult { 

     do{ 
      let jsonObject : [[String:AnyObject]] 
      = try NSJSONSerialization.JSONObjectWithData(data, options: []) as! [[String:AnyObject]] 

      for profileJSON in jsonObject { 
       if let profile = profileFromJsonObject(profileJSON) { 
        finalProfile.append(profile) 
       } 
      } 
      return .Success(finalProfile) 
     } 
     catch let error { 
      return .Failure(error) 
     } 

    } 

es ist meine profileFromJsonObject Methode für Parse-JSON in Profil Instanz

func profileFromJsonObject(json: [String:AnyObject]) -> UserProfile?{ 

     guard let 
      id = json["id"] as? Int, 
      name = json["name"] as? String, 
      userName = json["username"] as? String, 
      email = json["email"] as? String, 
      address = json["address"] as? NSDictionary, 
      phone = json["phone"] as? String, 
      website = json["website"] as? String, 
      company = json["company"] as? NSDictionary 
      else { 
       return nil 
     } 

    let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company) 
     return obj 
    } 
+0

Ich bin Aufruf profileFromJsonObject Funktion nicht profileFromJSONData. – ava

+0

in ProfilFromJsonObject Funktion Ich bin JSAR Array in eine Profilinstanz analysieren. – ava

+0

Zeigen Sie die Methode 'profileFromJsonObject' an. –

Antwort

1

ich Ihre JSON versuche, indem sie sie in lokaler Datei nehmen, und ich bin in der Lage in folgenden Weise zum Modell Objekt zu analysieren. Sie stellen Ihre Remote-JSON nur in meinem Code, ich hoffe, es

func getTheLocalJSON() 
{ 
    let filePath = NSBundle.mainBundle().pathForResource("test", ofType: "json"); 
    let data = NSData.init(contentsOfFile: filePath!); 

    var json : NSArray! 
    do{ 
     json = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSArray; 
    }catch{ 

    } 

    print("json \(json)"); 

    let dictResponse = json.objectAtIndex(0) as! NSDictionary; 

    let objUser = profileFromJsonObject(dictResponse)! as UserProfile; 

    print("user : \(objUser)"); 

    //Code to access company name and show it as screen title 
    self.title = (objUser.company)!["name"] as? String; 

    lblCompanyname.text = (objUser.company)!["name"] as? String; 
    lblCatchPhrase.text = (objUser.company)!["catchPhrase"] as? String; 
    lblbs.text = (objUser.company)!["bs"] as? String; 

} 

func profileFromJsonObject(json: NSDictionary) -> UserProfile?{ 

    guard let 
     id = json["id"] as? Int, 
     name = json["name"] as? String, 
     userName = json["username"] as? String, 
     email = json["email"] as? String, 
     address = json["address"] as? NSDictionary, 
     phone = json["phone"] as? String, 
     website = json["website"] as? String, 
     company = json["company"] as? NSDictionary 
     else { 
      return nil 
    } 

    let obj = UserProfile(id: id, name: name, userName: userName, email: email, address: address, phone: phone, website: website, company: company) 

    return obj 
} 

Output arbeiten:

enter image description here

+0

das funktioniert gut, aber wie Zugriff auf Adresse oder Firma Wörterbuch für dieses Detail in der Detailansicht zeigen? – ava

+0

So können Sie auf das Wörterbuch zugreifen: print ("Firmenname: \ ((objUser.company)! [" Name "])"); – Janmenjaya

+0

Ja, es ist der perfekte Weg, um auf die Wörterbuchdaten zuzugreifen und mein Problem zu lösen. – ava

Verwandte Themen