【问题标题】:Parsing the JSON iOS-Swift解析 JSON iOS-Swift
【发布时间】:2019-01-08 10:20:04
【问题描述】:

我在可编码模型的帮助下解析了一个 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\”).“, 底层错误: 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
}

【问题讨论】:

    标签: ios json swift alamofire codable


    【解决方案1】:

    您的 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"
            }]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多