我需要解析JSON數組中的10個數組並將其存取。我可以通過REST API獲取數據並在控制檯上打印出來,但我不知道如何實際保存它以處理。在swift中解析一個雙JSON編碼數組並訪問它
JSON陣列結構:
[[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge],
[lol_id, lol_time, lol_uid, lol_username, lol_type, lol_url, lol_title, lol_score, lol_comments, lol_badge]]
那些陣列基本上返回整數和字符串。
如何將這些保存爲相同樣式(數組中的數組)並訪問它們?
這裏是我的了代碼直到如今
@IBAction func buttonLoad(sender: AnyObject) {
// Setting up the URL
let myUrl = NSURL(string: "http://hugelol.com/api/front.php");
// Request
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil
{
println("error=\(error)")
return
}
// Prints server response
println("response = \(response)")
// Prints the double array
let responseString = NSString(data: data, encoding: NSUTF8StringEncoding)
println("responseString = \(responseString)")
}
task.resume()
}
我已經嘗試了許多不同的方法,但有很大的麻煩了解究竟是如何做到這一點。我也嘗試過swiftyJSON,但我仍然不知道如何將數據保存到快速數組中。我正在將我的頭撞到牆上。謝謝你的閱讀。
我有_SwiftyJSON_ 這樣的結果讓'海峽=「[大聲笑,大聲笑,Lol],[Lol,Lol,Lol]]''; 'let data = JSON(str)'; 'println(data); // [[Lol,Lol,Lol],[Lol,Lol,Lol]] –