使用Codable解析JSON数据 [英] Parsing the JSON data with Codable

查看:240
本文介绍了使用Codable解析JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可编码模型帮助解析的JSON响应。即使我的模型看起来不错,我也遇到以下错误,

I have a JSON response that I am parsing with the help of codable models. Even though my models look good, I am getting the below error,


失败:typeMismatch(Swift.Dictionary< Swift.String,Any> ,Swift.DecodingError.Context(codingPath:[CodingKeys(stringValue: data,intValue:nil)

FAILURE: typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil)

JSON响应为:

{
    "status": "success",
    "message": "successfully.",
    "user": {
        "username": "admin",
        "profileImage": "/storage/default.png"
    },
    "data": {
        "cash": {
            "withdrawableCash": "$99999910",
            "outstandingOrders": "$0"
        },
        "offering": [
            {
                "company": "TSLA",
                "location": "Location",
                "amount": 40
            },
            {
                "company": "TSLA",
                "location": "Location",
                "amount": 50
            }
        ],
        "history": [
            {
                "amount": 100000000,
                "order_id": 0,
                "order_type": "deposit",
                "status": 1,
                "message": "Added money into wallet",
                "transaction_time": "30-07-2018 18:10"
            },
            {
                "amount": 40,
                "order_id": 1,
                "order_type": "auctionBid",
                "status": 2,
                "message": "Placed bid with 4 notes, on Auction (TSLA)",
                "transaction_time": "30-07-2018 18:11"
            },
            {
                "amount": 50,
                "order_id": 2,
                "order_type": "auctionBid",
                "status": 2,
                "message": "Placed bid with 5 notes, on Auction (TSLA)",
                "transaction_time": "30-07-2018 18:11"
            }
        ]
    }
}

模型为:

public struct WalletResponseModel: Codable {
    public let status: String
    public let message: String
    public let user: UserData
    public let data: WalletData
}

public struct UserData: Codable {
    public let username: String
    public let profileImage: URL

    enum CodingKeys: String, CodingKey {
        case username
        case profileImage = "profileImage"
    }
}

public struct WalletData: Codable {
    public let cash: Cash
    public let history: [HistoryItem]
    public let offerings: [Offering]

    enum CodingKeys: String, CodingKey {
        case cash
        case history
        case offerings = "offering"
    }
}

public struct Cash: Codable {
    public let withdrawableCash: String
    public let outstandingOrders: String
}

public struct HistoryItem: Codable {
    public let amount: Int
    public let status: Int
    public let orderId: Int
    public let orderType: String
    public let message: String
    public let transactionTime: String

    enum CodingKeys: String, CodingKey {
        case amount, status, message
        case transactionTime = "transaction_time"
        case orderId = "order_id"
        case orderType = "order_type"
    }
}

public struct Offering: Codable {
    public let company: String
    public let amount: Int
    public let location: String
}


推荐答案

尝试创建像这样

struct Result:Decodable {
    let status: String
    let message: String
    let user:user
    let data: data

}
struct user:Decodable {
    let username: String
    let profileImage:String
}
struct data:Decodable {
    let cash:cash
    let offering:[offering]
    let history:[history]
}
struct cash:Decodable {
    let outstandingOrders:String
    let withdrawableCash: String
}
struct offering:Decodable {
    let location:String
    let company:String
    let amount:Int
}
struct history:Decodable {
    let amount: Int
    let message: String
    let orderId: Int
    let orderType: String
    let status:Int
    let transactionTime: String
}

并解码

do {
             let decoder = JSONDecoder()
            decoder.keyDecodingStrategy = .convertFromSnakeCase
            let response = try decoder.decode(Result.self, from: data)
            print(response)

        } catch {
            // handle error
        }

这篇关于使用Codable解析JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆