Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
199 views
in Technique[技术] by (71.8m points)

swift - Parsing the JSON data with Codable

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,

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

The JSON response is:

{
    "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"
            }
        ]
    }
}

The models are:

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I do not see an error. But previously when I had a similar problem I started with a small a hand full of elements in the data and the model and tested. Added one or two at a time until error occurred and could find the problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...