2017-05-07 6 views
-5

Nun habe ich eine Antwort des Servers mit diesem:Wie bekomme ich ein String-Array von JSON-Arrays?

{ 
cars =  (
{ 
"color" = red; 
"model" = ferrari; 
"othersAtributes" = others atributes; 
},{ 
"color" = blue; 
"model" = honda; 
"othersAtributes" = others atributes; 
},{ 
"color" = green; 
"model" = ford; 
"othersAtributes" = others atributes; 
},{ 
"color" = yellow; 
"model" = porshe; 
"othersAtributes" = others atributes; 
} 
) 
} 

Ich brauche eine Liste von Modellautos. eine Reihe von Modellen Autos, um eine Liste zu setzen.

Antwort

0

allererst Sie die rohe Antwort vom Server auf einen Swift konvertieren müssen Dictionary .

let payload = [ 
    "cars": [ 
    [ 
     "color": "red", 
     "model": "ferrari", 
     "othersAtributes": "others atributes" 
    ], 
    [ 
     "color": "blue", 
     "model": "honda", 
     "othersAtributes": "others atributes" 
    ], 
    [ 
     "color": "green", 
     "model": "ford", 
     "othersAtributes": "others atributes" 
    ], 
    [ 
     "color": "yellow", 
     "model": "porshe", 
     "othersAtributes": "others atributes" 
    ] 
    ] 
] 

Dann

let models: [String] = payload["cars"]!.map({ $0["model"] as! String }) 
print(models) 

werden Sie ["ferrari", "honda", "ford", "porshe"] geben.

(Sie könnten die Kraft ! auspackt mit sicheren Fehlerbehandlungsmechanismen erhalten mögen ersetzen.)

+0

Wie kann ich konvertieren? so was? 'Let Payload = Antwort als? [String: Irgendein] '? – steibour

+0

Ich nehme an, die Server-Nutzlast ist in Standard-JSON-Form und Sie fordern es mit 'NSURLSessionDataTask' an, so dass Sie ein' Data'-Objekt erhalten. Dann wird die Antwort sein, was @Vignesh J gepostet hat. Obwohl ich '[String: Any]' empfehlen würde, um schneller zu sein. 'let Nutzlast: [String: Beliebig] = NSJSONSerialization.JSONObjectWithData (Daten, Optionen: NSJSONReadingOptions.MutableContainers, Fehler: nil) as! [String: Any] ' – xiangxin

+0

Ich mache es mit Alamofire – steibour

0

Versuchen Sie dieses Snippet. Nicht perfekt getestet. Ich Autos bin zu raten, ist ein Array:

var modelsArray = [String]() 
     for index in cars.enumerated(){ 
      let model = cars[index].value(forKey: "model") 
      modelsArray.append(model) 
     } 
-1

Dieser Code für bekommen die Zeichenfolge json Array

func searchFunction(searchQuery: NSString) { 
     var url : NSURL = NSURL.URLWithString(" ENTER YOUR URL") 
     var request: NSURLRequest = NSURLRequest(URL:url) 
     let config = NSURLSessionConfiguration.defaultSessionConfiguration() 
     let session = NSURLSession(configuration: config) 

     let task : NSURLSessionDataTask = session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in 

      var newdata : NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary 

      var info : NSArray = newdata.valueForKey("cars") as NSArray 

      var color: String? = info.valueForKey("color") as? String 
      println(color) 


      var model: NSString = info.valueForKey("model") as NSString //Crashes 
      println(model) 

    var othersAtributes: NSString = info.valueForKey("othersAtributes") as NSString //Crashes 
      println(othersAtributes) 

      }); 


     task.resume() 


    } 
+0

siehe den Link: http://stackoverflow.com/questions/24074042/getting-values-from-json-array -in-swift –