Reputation: 47
I want to parse a json with variable keys. Here is the example:
"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"
}
...
}
Using SwiftyJSON i know how to convert a JSON with static keys. But i am not aware of any way to take date (2018-09-13 etc) as a Data Bean and create an Array of dates. What is the best way to handle this scenario?
The code i use for parsing the static keys in SwiftJSON is:
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.
}
}
Upvotes: 0
Views: 196
Reputation: 10012
json's root is a JsonObject i.e. Key and Value Date is the key and value is again a JsonObject. So it can be handled like
Code
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)")
})
}
}
}
output
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
Hope that helps :)
Upvotes: 1