我需要从Swift中的JSON文件url导入数字字符串。这样做最简单,最准系统的方法是什么?
我有一个JSON文件的网址
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和程序设计的新手。
答案 0 :(得分:0)
从服务器获取数据的简单方法是使用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)
}
您可以在操场上尝试上面的代码。
答案 1 :(得分:0)
尝试将以下物品放在操场上
: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协议的资源: