使用Codable在Swift 4.2中进行Json解析

时间:2019-02-24 15:44:45

标签: ios swift iphone codable decodable

我正在一个项目上,我从API收到了一个响应。现在我想通过 JsonDecode()。decod 函数使用可解码的类来获取响应。

我已经制作了可编码的结构,但是它给了我这样的错误:

由于数据格式不正确,因此无法读取。

我的API响应是:

{
    auth = 1;
    data =     {
        "dealer_rating" =         {
            average = "3.5";
            "average_width" = "70%";
            count = 2;
            dislikes = 0;
            likes = 2;
            rate1 = 4;
            "rate1_label" = "Atenci\U00f3n al cliente";
            "rate1_width" = "80%";
            rate2 = "3.5";
            "rate2_label" = "Proceso de compra";
            "rate2_width" = "70%";
            rate3 = 3;
            "rate3_label" = "Experiencia general";
            "rate3_width" = "60%";
        };
        review =         (
                        {
                average = 3;
                "post_author" = 16;
                "post_content" = "Test of iOS developer.";
                "post_title" = "iOS Test";
                rate1 = 3;
                "rate1_label" = "Atenci\U00f3n al cliente";
                rate2 = 3;
                "rate2_label" = "Proceso de compra";
                rate3 = 3;
                "rate3_label" = "Experiencia general";
                "stm_recommended" = yes;
                username = "";
            },
                        {
                average = 4;
                "post_author" = 22;
                "post_content" = vvvvvvvvvv;
                "post_title" = vvvvvvvvvv;
                rate1 = 5;
                "rate1_label" = "Atenci\U00f3n al cliente";
                rate2 = 4;
                "rate2_label" = "Proceso de compra";
                rate3 = 3;
                "rate3_label" = "Experiencia general";
                "stm_recommended" = yes;
                username = Vnn;
            }
        );
    };
    message = "Review found.";
    status = 1;
}

我的可分解结构为:

struct resDelerReview : Codable {
    struct struDataString : Codable {
        var average:String?
        var rate1:Int
        var rate1_label:String?
        var rate2:Int
        var rate2_label:String?
        var rate3:Int
        var rate3_label:String?
        var likes:Int
        var dislikes:Int
        var count:Int
        var rate1_width:String?
        var rate2_width:String?
        var rate3_width:String?
        var average_width:String?
    }

    struct struReviewList : Codable {
        var post_author:String?
        var username:String?
        var post_content:String?
        var post_title:String?
        var rate1:String?
        var rate1_label:String?
        var rate2:String?
        var rate2_label:String?
        var rate3:String?
        var rate3_label:String?
        var stm_recommended:String?
        var average:Int
    }


    struct reviewRating : Codable {
        let dealer_rating:struDataString
        let review:[struReviewList]
    }

    let data:reviewRating
    let status:Bool
    let auth:Bool
    let message:String?
}

邮件中的邮递员回复

{
    "data": {
        "dealer_rating": {
            "average": "3.5",
            "rate1": 4,
            "rate1_label": "Atención al cliente",
            "rate2": 3.5,
            "rate2_label": "Proceso de compra",
            "rate3": 3,
            "rate3_label": "Experiencia general",
            "likes": 2,
            "dislikes": 0,
            "count": 2,
            "rate1_width": "80%",
            "rate2_width": "70%",
            "rate3_width": "60%",
            "average_width": "70%"
        },
        "review": [
            {
                "post_author": "16",
                "username": "",
                "post_content": "Test of iOS developer.",
                "post_title": "iOS Test",
                "rate1": "3",
                "rate1_label": "Atención al cliente",
                "rate2": "3",
                "rate2_label": "Proceso de compra",
                "rate3": "3",
                "rate3_label": "Experiencia general",
                "stm_recommended": "yes",
                "average": 3
            },
            {
                "post_author": "22",
                "username": "Vnn",
                "post_content": "vvvvvvvvvv",
                "post_title": "vvvvvvvvvv",
                "rate1": "5",
                "rate1_label": "Atención al cliente",
                "rate2": "4",
                "rate2_label": "Proceso de compra",
                "rate3": "3",
                "rate3_label": "Experiencia general",
                "stm_recommended": "yes",
                "average": 4
            }
        ]
    },
    "status": true,
    "auth": true,
    "message": "Review found."
}

我的邮递员回复是:

enter image description here

请帮助我被卡住的人。

预先感谢

1 个答案:

答案 0 :(得分:0)

如果您阅读了全部内容,错误就很清楚了,它是属性rate2被声明为Int,但其中一个元素包含3.5

因此,将其在struDataString结构中更改为

var rate2: Double

注意 struDataString中还有其他几个类似rate2的字段,很可能它们可能包含double值来与其他rateX字段相似。您可能要查看所有文档的文档