【发布时间】:2026-02-15 15:25:03
【问题描述】:
我需要将一个数组中的 10 个数组从 JSON 解析为 swift 并访问它。我可以通过 REST API 获取数据并在控制台上打印出来,但我不知道如何实际保存它以进行处理。
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,但我仍然不知道如何将数据保存到 swift 数组中。我差点把头撞到墙上。感谢您的阅读。
【问题讨论】:
-
我有这样的结果 SwiftyJSON
let str = "[[Lol, Lol, Lol], [Lol, Lol, Lol]]";let data = JSON(str);println(data); // [[Lol, Lol, Lol], [Lol, Lol, Lol]]
标签: ios arrays json swift parsing