在Swift中将这个JSON数据导入数组的最简单方法是什么?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Swift中将这个JSON数据导入数组的最简单方法是什么?相关的知识,希望对你有一定的参考价值。
我需要从Swift中的JSON文件url导入一串数字。什么是最简单和最准确的方式?
我有一个JSON文件的URL
https://www.norsk-tipping.no/api-keno/getResultInfo.json?drawID=4178
它看起来像这样:
while(true);/* 0;
{"gameID":13,"drawID":4178,"drawDate":"2019,04,13,20,00,00","levels":[10,0,0,0,0,0,0,9,0,0,0,0,0,8,0,0,0,0,7,0,0,0,6,0,0,0,5,0,0,4,0,0,3,0,2],"wagers":[5,10,20,30,50,100],"correct":[10,9,8,7,6,5,0,9,8,7,6,5,0,8,7,6,5,4,7,6,5,4,6,5,4,3,5,4,3,4,3,2,3,2,2],"odds":[200000,5000,200,20,4,1,1,50000,1100,50,8,2,1,10000,240,20,3,1,2400,100,10,1,420,20,3,1,200,9,1,35,2,1,18,1,7],"winners":[0,5,27,196,959,3308,1556,1,0,8,56,219,294,1,3,59,305,1043,5,10,77,386,2,58,393,1340,16,108,548,44,433,1682,126,971,259],"drawNumbers":[1,13,23,30,40,42,43,48,49,50,51,52,53,54,55,56,60,61,63,65],"unsortedDrawNumbers":[63,52,13,56,53,30,54,23,65,55,61,1,51,42,60,43,48,49,50,40],"turnover":795865}
/* */
我需要从JSON文件中的“drawNumbers”变量中创建一个数组。
我该如何以最简单的方式做到这一点?我不需要任何零保护或任何东西,这只是为了理解导入JSON数据的工作原理。
对不起,请原谅我,因为我对Swift和一般编程都是全新的。
答案
从服务器获取数据的简单方法是使用URLSession
并考虑以下代码:
var request = URLRequest(url: URL(string: "https://www.norsk-tipping.no/api-keno/getResultInfo.json?drawID=4178")!)
request.httpMethod = "GET"
let session = URLSession.shared
session.dataTask(with: request) {data, response, err in
print("Entered the completionHandler")
if let data = data {
do {
let gameData = try JSONDecoder().decode(GameData.self, from: data)
print(gameData)
} catch {
print(error)
}
}
}.resume()
而Codable
struct
将如下所示:
import Foundation
struct GameData: Codable {
let gameID, drawID: Int
let drawDate: String
let levels, wagers, correct, odds: [Int]
let winners, drawNumbers, unsortedDrawNumbers: [Int]
let turnover: Int
}
使用上面的struct
,您可以解析数据。
或者你可以简单地使用像Alamofire这样的第三方库来处理服务器请求。
但重要的是你没有从服务器那里得到正确的JSON
while(true);/* 0;
和
/* */
不应该在那里。
如果您尝试手动尝试删除它,您可以从中获取drawNumbers
,如下所示:
let data = """
{
"gameID": 13,
"drawID": 4178,
"drawDate": "2019,04,13,20,00,00",
"levels": [
10,
0,
0,
0,
0,
0,
0,
9,
0,
0,
0,
0,
0,
8,
0,
0,
0,
0,
7,
0,
0,
0,
6,
0,
0,
0,
5,
0,
0,
4,
0,
0,
3,
0,
2
],
"wagers": [
5,
10,
20,
30,
50,
100
],
"correct": [
10,
9,
8,
7,
6,
5,
0,
9,
8,
7,
6,
5,
0,
8,
7,
6,
5,
4,
7,
6,
5,
4,
6,
5,
4,
3,
5,
4,
3,
4,
3,
2,
3,
2,
2
],
"odds": [
200000,
5000,
200,
20,
4,
1,
1,
50000,
1100,
50,
8,
2,
1,
10000,
240,
20,
3,
1,
2400,
100,
10,
1,
420,
20,
3,
1,
200,
9,
1,
35,
2,
1,
18,
1,
7
],
"winners": [
0,
5,
27,
196,
959,
3308,
1556,
1,
0,
8,
56,
219,
294,
1,
3,
59,
305,
1043,
5,
10,
77,
386,
2,
58,
393,
1340,
16,
108,
548,
44,
433,
1682,
126,
971,
259
],
"drawNumbers": [
1,
13,
23,
30,
40,
42,
43,
48,
49,
50,
51,
52,
53,
54,
55,
56,
60,
61,
63,
65
],
"unsortedDrawNumbers": [
63,
52,
13,
56,
53,
30,
54,
23,
65,
55,
61,
1,
51,
42,
60,
43,
48,
49,
50,
40
],
"turnover": 795865
}
""".data(using: .utf8)!
do {
let gameData = try JSONDecoder().decode(GameData.self, from: data)
print(gameData.drawNumbers) //[1, 13, 23, 30, 40, 42, 43, 48, 49, 50, 51, 52, 53, 54, 55, 56, 60, 61, 63, 65]
} catch {
print(error)
}
你可以在游乐场尝试上面的代码。
另一答案
尝试将以下内容放在游乐场中:
let sampleResponse = """
{"gameID":13,"drawID":4178,"drawDate":"2019,04,13,20,00,00","levels":[10,0,0,0,0,0,0,9,0,0,0,0,0,8,0,0,0,0,7,0,0,0,6,0,0,0,5,0,0,4,0,0,3,0,2],"wagers":[5,10,20,30,50,100],"correct":[10,9,8,7,6,5,0,9,8,7,6,5,0,8,7,6,5,4,7,6,5,4,6,5,4,3,5,4,3,4,3,2,3,2,2],"odds":[200000,5000,200,20,4,1,1,50000,1100,50,8,2,1,10000,240,20,3,1,2400,100,10,1,420,20,3,1,200,9,1,35,2,1,18,1,7],"winners":[0,5,27,196,959,3308,1556,1,0,8,56,219,294,1,3,59,305,1043,5,10,77,386,2,58,393,1340,16,108,548,44,433,1682,126,971,259],"drawNumbers":[1,13,23,30,40,42,43,48,49,50,51,52,53,54,55,56,60,61,63,65],"unsortedDrawNumbers":[63,52,13,56,53,30,54,23,65,55,61,1,51,42,60,43,48,49,50,40],"turnover":795865}
"""
你有一个奇怪的日期格式,所以你需要想出一个日期格式化程序来处理它,我在下面写的...
extension DateFormatter {
static let weirdDateFormat: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy,MM,dd,HH,mm,ss"
formatter.calendar = Calendar(identifier: .gregorian)
// add time zone, etc. here
return formatter
}()
}
接下来,我们使用Codable结构来解码数据。以下是我根据您提供的示例提出的内容。
struct ResponseObject: Codable {
let gameID: Int
let drawID: Int
let drawDate: Date // Correct this to date
let levels: [Int]
let wagers: [Int]
let correct: [Int]
let odds: [Int]
let winners: [Int]
let drawNumbers: [Int]
let unsortedDrawNumbers: [Int]
let turnover: Int
}
现在,我们解码JSON。你会注意到我使用上面的weirdDateFormat
扩展名来格式化dateDecodingStrategy。
let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .formatted(.weirdDateFormat)
if let data = sampleResponse.data(using: .utf8) {
do {
let response = try jsonDecoder.decode(ResponseObject.self, from: data)
print(response)
} catch {
print(error)
}
}
以下是使用Codable协议的一些资源:
以上是关于在Swift中将这个JSON数据导入数组的最简单方法是什么?的主要内容,如果未能解决你的问题,请参考以下文章