gpt4 book ai didi

ios - JS 对象到 Swift 结构

转载 作者:搜寻专家 更新时间:2023-11-01 06:53:31 25 4
gpt4 key购买 nike

我在将数据(从端点获取的类似 JSON)解析为 Swift 结构时遇到困难。因为我从端点获得的数据似乎不是有效的 JSON(至少不是所有的数据都在查看 object = (...) 的结构),所以我无法解码 列表结构

我应该用另一种方式解析它吗?非常感谢任何建议

我准备的结构是:

struct Response:Codable {
let message:String?
let list:ListStruct?
let error:Bool?
}

struct ListStruct:Codable {
let object1:[Object1]?
let object2:[Object2]?
let object3:[Object3]?
}

struct Object1:Codable {
id:Int?
name:String?
}
...

我从端点获得的数据示例:

["message": <null>, "list": {
object1 = (
{
id = 1;
name = "testing1";
}
);
object2 = (
{
files = (
);
id = 1;
name = "testing2-1";
photos = (
);
},
{
files = (
);
id = 2;
name = "testing2-2";
photos = (
);
systemId = 8;
}
);
object3 = (
{
id = 6;
name = "testing3-1";
},
{

id = 13;
name = "testing3-2";
}
);
}, "error": 0]

编辑

我是如何尝试解码的:

if let result = try JSONDecoder().decode(Response?.self, from: "\(response!)".data(using: .utf8)! ) {
print("\(result)")
}

我得到的错误:

Error: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid JSON.", underlyingError: Optional(Error Domain=NSCocoaErrorDomain Code=3840 "No string key for value in object around character 6." UserInfo={NSDebugDescription=No string key for value in object around character 6.})))

最佳答案

很可能,您通过使用字符串插值创建传递了错误的数据对象。如果 response 类型是 Data 那么你不需要在下面的行中重新创建它,

if let result = try JSONDecoder().decode(Response?.self, from: "\(response!)".data(using: .utf8)! ) {

尝试按原样传递response。如下所示,

if let result = try JSONDecoder().decode(Response?.self, from: response!) {

这是一个完整的可测试示例,其中使用有问题的 json 创建了正确的数据对象,并且 Response 中的 error 类型从可选 BoolInt,

struct Response:Codable {
let message:String?
let list:ListStruct?
let error: Int?
}

struct ListStruct: Codable {
let object1:[Object1]?
let object2:[Object2]?
let object3:[Object3]?
}

struct Object1: Codable {
var id:Int?
var name:String?
}

struct Object2: Codable {
var id:Int?
var name:String?
var systemId: Int?
}

struct Object3: Codable {
var id:Int?
var name:String?
}

用法:

let data = """
{"message": null,

"list": {
"object1": [
{
"id": 1,
"name": "testing1"
}
],
"object2" : [
{
"files" : [
],
"id" : 1,
"name" : "testing2-1",
"photos" : [
]
},
{
"files" : [
],
"id" : 2,
"name" : "testing2-2",
"photos" : [
],
"systemId" : 8
}
],
"object3" : [
{
"id" : 6,
"name" : "testing3-1",
},
{

"id" : 13,
"name" : "testing3-2",
}
]
},

"error": 0
}
""".data(using: .utf8)!

if let result = try! JSONDecoder().decode(Response?.self, from: data) {
result.list?.object1?.forEach({ obj in
print(obj.name)
})
result.list?.object2?.forEach({ obj in
print(obj.name)
})
result.list?.object3?.forEach({ obj in
print(obj.name)
})
}

输出:

Optional("testing1")
Optional("testing2-1")
Optional("testing2-2")
Optional("testing3-1")
Optional("testing3-2")

关于ios - JS 对象到 Swift 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55452939/

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