2015-02-06 12 views
24

Ich habe eine json, die ich mit SwiftyJSON analysieren konnte:Wie durchläuft JSON mit SwiftyJSON?

if let title = json["items"][2]["title"].string { 
    println("title : \(title)") 
} 

funktioniert perfekt.

Aber ich konnte es nicht durchlaufen. Ich versuchte zwei Methoden, die erste ist

// TUTO : 
//If json is .Dictionary 
for (key: String, subJson: JSON) in json { 
    ... 
} 
// WHAT I DID : 
for (key: "title", subJson: json["items"]) in json { 
    ... 
} 

XCode hat die for-Schleife Erklärung nicht akzeptieren.

Die zweite Methode:

// TUTO : 
if let appArray = json["feed"]["entry"].arrayValue { 
    ... 
} 
// WHAT I DID : 
if let tab = json["items"].arrayValue { 
    ... 
} 

XCode nicht akzeptieren, die if-Anweisung.

Was mache ich falsch?

Antwort

60

Wenn Sie Schleife json["items"] durch Array wollen, versuchen Sie:

for (key, subJson) in json["items"] { 
    if let title = subJson["title"].string { 
     println(title) 
    } 
} 

Was die zweite Methode, .arrayValue kehrt nichtOptional Array, Sie .array stattdessen verwenden sollten :

if let items = json["items"].array { 
    for item in items { 
     if let title = item["title"].string { 
      println(title) 
     } 
    } 
} 
+0

Wenn ich versuche, mein Wörterbuch von [String: JSON] zu durchlaufen, erhalte ich den Fehler: '[String: JSON]?' hat kein Mitglied namens "Generator" – mattgabor

+0

, weil es optional ist – nikans

+0

@rintaro Ich habe Ihre Methode verwendet und es funktioniert, aber ich bin nur nicht sicher, ob ich effizient bin oder nicht.Wenn Sie mehrere 'Items' zum Erfassen hätten (zB Titel, Autor, Kommentar) würden Sie einfach' if > = item ["<>"]. String {} 'für jeden von ihnen wiederholen? –

5

In der for-Schleife kann der Typ key nicht vom Typ "title" sein. Da "title" eine Zeichenfolge ist, gehen Sie für: key:String. Und dann können Sie innerhalb der Schleife speziell "title" verwenden, wenn Sie es brauchen. Und auch der Typ subJson muss JSON sein.

Und da eine JSON-Datei als 2D-Array betrachtet werden kann, gibt json["items'].arrayValue mehrere Objekte zurück. Es ist sehr ratsam zu verwenden: if let title = json["items"][2].arrayValue.

Werfen Sie einen Blick auf: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Types.html

+0

Bitte kreuzen Sie, dass grünes Etikett auf der linken Seite, wenn ich geholfen. Vielen Dank! –

2

Überprüfen Sie bitte die README

//If json is .Dictionary 
for (key: String, subJson: JSON) in json { 
    //Do something you want 
} 

//If json is .Array 
//The `index` is 0..<json.count's string value 
for (index: String, subJson: JSON) in json { 
    //Do something you want 
} 
+1

Er sagte in der Frage, dass er die Dokumente überprüft. Nicht sehr hilfreich imo. Es tut uns leid. –

+0

Das hat mir ziemlich geholfen. Danke – derickito

7

Ich finde es ein bisschen seltsam erklärt mir, denn eigentlich mit:

for (key: String, subJson: JSON) in json { 
    //Do something you want 
} 

gibt Syntaxfehler (in Swift 2.0 atleast)

richtig war:

In der Tat Schlüssel ist eine Zeichenfolge und subJson ist ein JSON-Objekt.

Jedoch habe ich es ein wenig anders zu tun, hier ist ein Beispiel:

//jsonResult from API request,JSON result from Alamofire 
    if let jsonArray = jsonResult?.array 
    { 
     //it is an array, each array contains a dictionary 
     for item in jsonArray 
     { 
      if let jsonDict = item.dictionary //jsonDict : [String : JSON]? 
      { 
       //loop through all objects in this jsonDictionary 
       let postId = jsonDict!["postId"]!.intValue 
       let text = jsonDict!["text"]!.stringValue 
       //...etc. ...create post object..etc. 
       if(post != nil) 
       { 
        posts.append(post!) 
       } 
      } 
     } 
    } 
+0

Die richtige Syntax ist für (Schlüssel, subJson) :(String, JSON) in JSON – Emptyless

Verwandte Themen