解析JSON Swift 4

用户名

Swift 4 JSON解析Alamofire

我在解析JSON时遇到一些问题。错误:typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [], debugDescription: "Not an array", underlyingError: nil))我不知道该怎么做,也许是我的结构有问题。

结构体:

struct FinDataSample: Codable {
    let data: [dataSample]
}

struct dataSample : Codable {
    let k1: Int
    let k2: Int
    let k3: Int
    let k4: Int
    let name: String
    let val: [Int]
    let yy: Int
    let b: b

}

struct b: Codable{
    let k1: Int
    let k2: Int
    let k3: Int
    let k4: Int
    let name: String
    let val: [Int]
    let yy: Int
}

我通过Alamofire请求获取JSON:

let parameters: Parameters = [
            "idc": cinemaID,
            "yy": year,
            "func": mod
        ]
        request("https://placeholder.ru/test.php", method: .post, parameters: parameters, encoding: URLEncoding.default).debugLog().responseJSON { responseJSON in

            switch responseJSON.result {
            case .success(let value):
                print(value)
                do {
                   let parsedData = try FirebaseDecoder().decode([FinDataSample].self, from: value)
                   print(parsedData)

                   } catch let error {
                       print(error)
                   }
            case .failure(let error):
                print(error)
            }

        }

JSON:

{
    "data": [
        {
            "name": "Оборот кино", 
            "val": [
                8105782,
                5038389,
                5760657,
                3776628,
                4585381
            ],
            "yy": 27266837, 
            "k4": 0, 
            "k3": 0, 
            "k2": 8362009,
            "k1": 18904828, 

            "b": { 
                "name": "Оборот кино",
                "val": [ 
                    9944452,
                    5836494,
                    6562452,
                    4918204,
                    5773736
                ],
                "yy": 71047036, 
                "k4": 17195692, 
                "k3": 15293302,
                "k2": 16214644,
                "k1": 22343398
            }
        },

        {
            "name": "Количество людей",
            "val": [
                40981,
                27177,
                31634,
                21936,
                25757,
            ],
            "yy": 147485,
            "k4": 0,
            "k3": 0,
            "k2": 47693,
            "k1": 99792,
            "b": {
                "name": "Количество людей",
                "val": [
                    50737,
                    31379,
                    35282,
                    27476,
                    31379
                ],
                "yy": 390286,
                "k4": 94137,
                "k3": 88517,
                "k2": 90234,
                "k1": 117398
            }    
        },
        {
            "name": "Оборот бара",
            "val": [
                2820749,
                1709163,
                1940664,
                1355697,
                0
            ],
            "yy": 7826273,
            "k4": 0,
            "k3": 0,
            "k2": 1355697,
            "k1": 6470576,
            "b": {
                "name": "Оборот бара",
                "val": [
                    3606335.22,
                    2112999.1,
                    2375819.32,
                    1778227.49,
                    2089521.33
                ],
                "yy": 25702660.21,
                "k4": 6221608.47,
                "k3": 5522539.02,
                "k2": 5863359.08,
                "k1": 8095153.64
            }
        },
        {
            "name": "Количество чеков",
            "val": [
                13742,
                9079,
                10198,
                7177,
                0
            ],
            "yy": 40196,
            "k4": 0,
            "k3": 0,
            "k2": 7177,
            "k1": 33019,
            "b": {
                "name": "Количество чеков",
                "val": [
                    17506.48,
                    10257.28,
                    11533.1,
                    8632.17,
                    10143.31
                ],
                "yy": 124770.2,
                "k4": 30201.99,
                "k3": 26808.44,
                "k2": 28462.91,
                "k1": 39296.86
            }
        }
    ],
    "message": "loadm"
}

我需要解析:每个k1-k4,yy,val [],名称。

卡姆兰

FinDataSample不是array,只是改变[FinDataSample].selfFinDataSample.self

let parsedData = try FirebaseDecoder().decode(FinDataSample.self, from: value)

parsedData.data.forEach { sample in
    print(sample.k1)
    print(sample.k2)
    print(sample.k3)
    print(sample.k4)
    sample.val.forEach({ value in
        print(value)
    })
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章