gpt4 book ai didi

json - 在 Swift 中解析 json blob 字段

转载 作者:行者123 更新时间:2023-11-30 11:06:18 24 4
gpt4 key购买 nike

我正在 swift 中从 Web 服务读取 JSON,其格式如下

[{
"id":1,
"shopName":"test",
"shopBranch":"main",
"shopAddress":"usa",
"shopNumber":"5555555",
"logo":[-1,-40,-1,-32],
"shopPath":"test"
},
{
"id":2,
"shopName":"test",
"shopBranch":"main",
"shopAddress":"usa",
"shopNumber":"66666666",
"logo":[-1,-50,-2,-2],
"shopPath":"test"
}]

我已经成功地轻松读取了所有字符串,但是当涉及到 Logo 部分时,我不知道该怎么办,这是 mySQL 数据库中的一个 blob 字段,它代表我想要检索的图像我的快速用户界面,这是我执行此操作的代码,但我不断收到错误,并且无法找到正确的方法:

struct Brand: Decodable {
private enum CodingKeys: String, CodingKey {
case id = "id"
case name = "shopName"
case branch = "shopBranch"
case address = "shopAddress"
case phone = "shopNumber"
case logo = "logo"
case path = "shopPath"
}
let id: Int
let name: String
let branch: String
let address: String
let phone: String
let logo: [String]
let path: String
}
func getBrandsJson() {
let url = URL(string: "http://10.211.55.4:8080/exam/Test")
URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) in
guard let data = data, error == nil else {
print(error!);
return
}
print(response.debugDescription)
let decoder = JSONDecoder()
let classes = try! decoder.decode([Brand].self, from: data)
for myClasses in classes {
print(myClasses.branch)
if let imageData:Data = myClasses.logo.data(using:String.Encoding.utf8){
let image = UIImage(data:imageData,scale:1.0)
var imageView : UIImageView!
}
}
}).resume()
}

有人可以解释如何以正确的方式做到这一点,我已经搜索了很多但没有运气

最佳答案

首先,您最好告诉 Web 服务的服务器端工程师,使用数字数组返回 JSON 中的二进制数据效率不高,他们应该使用 Base-64 或类似的东西。

<小时/>

如果他们顽固地忽略您的建议,您最好在自定义解码初始值设定项中将其解码为 Data

struct Brand: Decodable {
private enum CodingKeys: String, CodingKey {
case id = "id"
case name = "shopName"
case branch = "shopBranch"
case address = "shopAddress"
case phone = "shopNumber"
case logo = "logo"
case path = "shopPath"
}
let id: Int
let name: String
let branch: String
let address: String
let phone: String
let logo: Data
let path: String

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(Int.self, forKey: CodingKeys.id)
self.name = try container.decode(String.self, forKey: CodingKeys.name)
self.branch = try container.decode(String.self, forKey: CodingKeys.branch)
self.address = try container.decode(String.self, forKey: CodingKeys.address)
self.phone = try container.decode(String.self, forKey: CodingKeys.phone)
self.path = try container.decode(String.self, forKey: CodingKeys.path)
//Decode the JSON array of numbers as `[Int8]`
let bytes = try container.decode([Int8].self, forKey: CodingKeys.logo)
//Convert the result into `Data`
self.logo = Data(bytes: bytes.lazy.map{UInt8(bitPattern: $0)})
}
}

您可以将 getBrandsJson() 的数据解码部分编写为:

        let decoder = JSONDecoder()
do {
//You should never use `try!` when working with data returned by server
//Generally, you should not ignore errors or invalid inputs silently
let brands = try decoder.decode([Brand].self, from: data)
for brand in brands {
print(brand)
//Use brand.logo, which is a `Data`
if let image = UIImage(data: brand.logo, scale: 1.0) {
print(image)
//...
} else {
print("invalid binary data as an image")
}
}
} catch {
print(error)
}

我写了一些行来通过猜测将数字数组解码为数据。因此,如果您发现我的代码不适用于您的实际数据,请告诉我足够的描述和一些实际数据的示例。 (至少,您需要向我展示实际 "logo" 数组中的前几百个元素。)

关于json - 在 Swift 中解析 json blob 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52678953/

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