2017-02-08 10 views
-1
self.manager!.request(url+"FindNearestClasses", method: .post, parameters: requestDictionary, encoding: URLEncoding.httpBody,headers: headers 
      ).responseJSON { response in 
       print(response.result.value) 
     } 

Das Problem, das ich habe die JSON zurück zu mir ist wie folgt. Wie kann ich es durch Nummer analysieren, die Array erhalten, und dann nach Array jeweils erste zweite dritte und vierte Zahlen lesen.Alamofire JSON Parsing

[ [ "1" "2" "3" 4], [ "5" "6" "7" 8] ]

Antwort

0
let myjson = [ [ "1", "2", "3", "4"], [ "5", "6", "7", "8"] ] 
let myInt = Int(myjson[0].first as! String) 
/* 
let i = 0 
let j = 0 
let myInt = Int(myjson[i][j] as! String) 
i stands for the which array 0 or 1 in this case. j stands for the value inside that array. 

*/ 
print(myInt) 

Dies wird 1 als Int, wenn Sie durch das gesamte Array Schleife dann For-Schleife verwenden möchten.

for myi in myjson{ 
for myj in myi { 
    print(myj) // this will print all the contents of both array. 
} 
} 

Zeichenfolge Int zu konvertieren, verwenden _ = Int(myjson[i][j] as! String)

0

Der beste Weg, um ein Werkzeug zu benutzen, ist die Antwort in schnellen Objekte abzubilden, ist ein Beispiel Object Mapper: https://github.com/tristanhimmelman/AlamofireObjectMapper

import AlamofireObjectMapper 

let URL = "https://raw.githubusercontent.com/tristanhimmelman/AlamofireObjectMapper/d8bb95982be8a11a2308e779bb9a9707ebe42ede/sample_json" 
Alamofire.request(URL).responseObject { (response: DataResponse<WeatherResponse>) in 

let weatherResponse = response.result.value 
print(weatherResponse?.location) 

if let threeDayForecast = weatherResponse?.threeDayForecast { 
    for forecast in threeDayForecast { 
     print(forecast.day) 
     print(forecast.temperature)   
    } 
    } 
}