适用于iOS的SwiftyJson中带有可变日期的JSON

问题描述 投票:0回答:1

我想用变量键解析一个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.
        }
    }
ios json swift swifty-json
1个回答
1
投票

json的根是JsonObject,即Key和Value Date是键,值也是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

希望有帮助:)

© www.soinside.com 2019 - 2024. All rights reserved.