gpt4 book ai didi

ios - 使用 Codable 在 swift 4.2 中解析 Json

转载 作者:行者123 更新时间:2023-11-29 05:59:12 25 4
gpt4 key购买 nike

我正在开发一个项目。我从 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?
}

postman 以文本形式回复

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

我的 postman 回复是:

enter image description here

请帮助我,我陷入困境。

提前致谢

最佳答案

如果您阅读整个错误,错误就非常清楚了,属性 rate2 被声明为 Int,但其中一个元素包含 3.5

因此将 struDataString 结构中的它更改为

var rate2: Double

注意还有其他几个类似的字段,例如 struDataString 中的rate2,这些字段很可能包含 double 值,就像其他rateX字段。您可能想查看所有这些的文档

关于ios - 使用 Codable 在 swift 4.2 中解析 Json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54853585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com