解析 JSON Swift 4

Posted

技术标签:

【中文标题】解析 JSON Swift 4【英文标题】:Parse JSON Swift 4 【发布时间】:2018-12-26 08:00:14 【问题描述】:

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[], name。

【问题讨论】:

【参考方案1】:

FinDataSample 不是array,只需将[FinDataSample].self 更改为FinDataSample.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)
    )

【讨论】:

谢谢你,哦,天哪,我试过了,但现在可以了)你能帮我吗,我怎样才能把这个值变成变量? 好的,谢谢,只需要添加:parsedData.data.forEach 是的,错过了。更新了!

以上是关于解析 JSON Swift 4的主要内容,如果未能解决你的问题,请参考以下文章

用 Codable,swift 4 解析 JSON

在 SWIFT 4 中解析嵌套的 JSON

Swift 4 JSON 解析指南

解析json xcode swift 4

在 Swift 4 中使用 Decodable 解析 JSON

解析 JSON 数据时出错(Swift 4 Playground)