JSON iOS 在 SwiftyJson 中具有可变日期
JSON with variable dates in SwiftyJson for iOS
我想用可变键解析 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,即键和值
日期是键,值也是一个 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("\([=10=]) : \()")
})
}
}
}
输出
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
希望对您有所帮助:)
我想用可变键解析 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,即键和值 日期是键,值也是一个 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("\([=10=]) : \()")
})
}
}
}
输出
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
希望对您有所帮助:)