Unable to decode a JSON with JSON Decoder

此生再无相见时 提交于 2019-12-11 04:19:29

问题


I'm using JSON decoder to decode some Instagram users. Here's the model:

struct User: Decodable {

    var clear_client_cache: Int
    var has_more: Int
    var hashtags: String?
    var places: String?
    var rank_token: String?
    var status: String?
    var users: [Users]?

     struct Users: Decodable {

        var position: Int
        var user: InstaUser?

         struct InstaUser: Decodable {

            var byline: String?
            var follower_count: Int?
            var full_name: String?
            var has_anonymous_profile_picture: Int?
            var is_verified: Int?
            var mutual_followers_count: Int?
            var pk: Int?
            var profile_pic_id: String?
            var profile_pic_url: String?
            var unseen_count: Int?
            var usernam: String?

        }

    }

}

    {
    "clear_client_cache" = 0;
    "has_more" = 0;
    hashtags =     (
    );
    places =     (
    );
    "rank_token" = "3b22a104-7ac5-4052-be10-e7a7be4e67b4";
    status = ok;
    users =     (
    );
}

typeMismatch(Swift.Int, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "clear_client_cache", intValue: nil)], debugDescription: "Expected to decode Int but found a number instead.", underlyingError: nil))

Than I just use

let user = try? decoder.decode(User.self, from: response.data!)

It says that it wants a number, what's wrong? Sorry, maybe it's too simple, but I'm coding for 12 hours already

来源:https://stackoverflow.com/questions/49826140/unable-to-decode-a-json-with-json-decoder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!