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

私が帰ってきた問題は以下の通りです。どのように配列を取得する番号でそれを解析することができますし、配列の後にそれぞれ最初の2番目の3番目と4番目の数字を読み取ります。Alamofire json parsing

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

答えて

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) 

これは、配列全体をループしてからforループを使用する場合は、1をIntとして出力します。

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

の例では、オブジェクトマッパーで、最良の方法は、迅速なオブジェクトに応答をマッピングするためのツールを使用することです_ = Int(myjson[i][j] as! String)

0

を使用し、intに文字列を変換するには: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)   
    } 
    } 
}