ios - 解析 JSON iOS-Swift

标签 ios json swift alamofire codable

我有一个 JSON 响应,我正在借助可编码模型对其进行解析。

尽管我的模型看起来不错,但我收到以下错误:

[Result]: FAILURE: keyNotFound(CodingKeys(stringValue: “user”, intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: “No value associated with key CodingKeys(stringValue: \“user\“, intValue: nil) (\“user\“).“, underlyingError: nil))

JSON 响应是:

{
   “status”: “success”,
   “message”: “successfully.“,
   “user”: {
       “username”: “admin”,
       “profileImage”: “/storage/default.png”
   },
   “data”: {
       “cash”: {
           “withdrawableCash”: “$999540”,
           “outstandingOrders”: “$0”
       },
       “offering”: [
           {
               “company”: “ABCD”,
               “location”: “San Francisco, CA”,
               “amount”: 10
           }
       ],
       “history”: [
           {
               “amount”: 330,
               “order_id”: 3,
               “order_type”: “preBid”,
               “status”: 2,
               “message”: “Placed bid with 33 notes, on Auction,
               “transaction_time”: “31-07-2018 05:31"
           }
       ]
   }
}

模型是:

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
}

最佳答案

您的 json 格式不正确它包含 “” 而不是 "" ,有效格式是

{
    "status": "success",
    "message": "successfully.",
    "user": {
        "username": "admin",
        "profileImage": "/storage/default.png"
    },
    "data": {
        "cash": {
            "withdrawableCash": "$999540",
            "outstandingOrders": "$0"
        },
        "offering": [{
            "company": "ABCD",
            "location": "San Francisco, CA",
            "amount": 10
        }],
        "history": [{
            "amount": 330,
            "order_id": 3,
            "order_type": "preBid",
            "status": 2,
            "message": "Placed bid with 33 notes, on Auction",
            "transaction_time": "31-07-2018 05:31"
        }]
    }
}

关于ios - 解析 JSON iOS-Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51623215/

相关文章:

iOS ViewController uilabel 仅在标签栏应用程序中旋转时更新

ios - 如何使用 Voice Over 确定当前聚焦的 UITableViewCell 的索引路径?

ios - 从应用商店安装后是否可以获取链接内容?

python - JSON 解析错误 "NoneType object is not callable"?

ios - 在屏幕上不可见时删除 SKNode

c# - 单点触控饼图

ios - 在边界框内查询经/纬度的数据库/文件?谷歌地图

php - iPhone 手机 safari JSON 解析错误

javascript - 带有 javascript 的 Swift WKWebView 界面未运行

objective-c - tvOS - UIButton 在聚焦时不动画