gpt4 book ai didi

ios - iOS 版 SwiftyJson 中具有可变日期的 JSON

转载 作者:行者123 更新时间:2023-11-28 14:13:14 24 4
gpt4 key购买 nike

我想解析一个带有可变键的 json。这是示例:

"Time Series (Daily)": {
"2018-09-14": {
"1. open": "113.3600",
"2. high": "113.7300",
"3. low": "112.4400",
"4. close": "113.3700",
"5. adjusted close": "113.3700",
"6. volume": "19122349",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-09-13": {
"1. open": "112.1200",
"2. high": "113.7250",
"3. low": "112.1200",
"4. close": "112.9100",
"5. adjusted close": "112.9100",
"6. volume": "26055620",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-09-12": {
"1. open": "111.4300",
"2. high": "111.8500",
"3. low": "110.5100",
"4. close": "111.7100",
"5. adjusted close": "111.7100",
"6. volume": "18891064",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
}
...
}

使用 SwiftyJSON 我知道如何使用静态键转换 JSON。但我不知道有什么方法可以将日期(2018-09-13 等)作为数据 Bean 并创建日期数组。处理这种情况的最佳方法是什么?

我用于解析 SwiftJSON 中的静态键的代码是:

func get<T>(url: String, queryParams: Parameters!, onComplete: @escaping (T?, Error?) -> ()) where T : Codable {
URLCache.shared.removeAllCachedResponses()
Alamofire.request(url, method: .get, parameters: queryParams, encoding: URLEncoding.default, headers: self.headers!).responseString {
response in
print("URL: \(url)")

switch response.result {
case .success(let value):
let statusCode = response.response?.statusCode
if (statusCode == Constant.httpSuccessCode){
print("Value: \(value)")
let json: String = JSON(value).string!
let jsonObj : T! = StringManager.decode(stringRepresentation: json)
onComplete(jsonObj, nil)
}else if(statusCode == Constant.httpInternalServerErrorCode){
//Error handling
}else if(statusCode == Constant.httpForbiddenCode || statusCode == Constant.httpUnAuthorizedCode){
//error handling
}else {
//error handling
}

case .failure(let error):
//error handling
}
//response is the json.
}
}

最佳答案

json 的根是一个 JsonObject 即 Key 和 Value日期是键,值也是一个 JsonObject。所以可以这样处理

代码

import SwiftyJSON
class Test{

let s = """

{"Time Series (Daily)": {
"2018-09-14": {
"1. open": "113.3600",
"2. high": "113.7300",
"3. low": "112.4400",
"4. close": "113.3700",
"5. adjusted close": "113.3700",
"6. volume": "19122349",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-09-13": {
"1. open": "112.1200",
"2. high": "113.7250",
"3. low": "112.1200",
"4. close": "112.9100",
"5. adjusted close": "112.9100",
"6. volume": "26055620",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
},
"2018-09-12": {
"1. open": "111.4300",
"2. high": "111.8500",
"3. low": "110.5100",
"4. close": "111.7100",
"5. adjusted close": "111.7100",
"6. volume": "18891064",
"7. dividend amount": "0.0000",
"8. split coefficient": "1.0000"
}
}
}
"""
func parseJSON(){

let j = JSON(parseJSON: s)
let timeSeries = j["Time Series (Daily)"]
for (key,json) in timeSeries{
print("\(key):")
json.forEach({
print("\($0) : \($1)")
})
}

}

}

输出

   2018-09-14:
1. open : 113.3600
4. close : 113.3700
8. split coefficient : 1.0000
5. adjusted close : 113.3700
3. low : 112.4400
7. dividend amount : 0.0000
2. high : 113.7300
6. volume : 19122349
2018-09-12:
1. open : 111.4300
4. close : 111.7100
8. split coefficient : 1.0000
5. adjusted close : 111.7100
3. low : 110.5100
7. dividend amount : 0.0000
2. high : 111.8500
6. volume : 18891064
2018-09-13:
1. open : 112.1200
4. close : 112.9100
8. split coefficient : 1.0000
5. adjusted close : 112.9100
3. low : 112.1200
7. dividend amount : 0.0000
2. high : 113.7250
6. volume : 26055620

希望对您有所帮助:)

关于ios - iOS 版 SwiftyJson 中具有可变日期的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52360345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com