gpt4 book ai didi

ios - JSON 解析,嵌套 JSON 结构问题

转载 作者:行者123 更新时间:2023-11-28 14:36:54 27 4
gpt4 key购买 nike

假设我们有这个 URL ,我正在使用 CodableAlamofire获取并解析 JSON 响应。

对于上述 URL 响应,我创建了以下 Codable 类。

struct CoinList: Codable {
let raw: Raw
let display: Display

enum CodingKeys: String, CodingKey {
case raw = "RAW"
case display = "DISPLAY"
}
}

struct Display: Codable {
let usd: [String: Usd]

enum CodingKeys: String, CodingKey {
case usd = "USD"
}
}

struct Raw: Codable {
let usd: [String: Usd]

enum CodingKeys: String, CodingKey {
case usd = "USD"
}
}

struct Usd: Codable {
let type, market, fromsymbol, tosymbol: String
let flags: String
let price: Double
let lastupdate: Int
let lastvolume, lastvolumeto: Double
let lasttradeid: String
let volumeday, volumedayto, volume24Hour, volume24Hourto: Double
let openday, highday, lowday, open24Hour: Double
let high24Hour, low24Hour: Double
let lastmarket: String
let change24Hour, changepct24Hour, changeday, changepctday: Double
let supply, mktcap, totalvolume24H, totalvolume24Hto: Double

enum CodingKeys: String, CodingKey {
case type = "TYPE"
case market = "MARKET"
case fromsymbol = "FROMSYMBOL"
case tosymbol = "TOSYMBOL"
case flags = "FLAGS"
case price = "PRICE"
case lastupdate = "LASTUPDATE"
case lastvolume = "LASTVOLUME"
case lastvolumeto = "LASTVOLUMETO"
case lasttradeid = "LASTTRADEID"
case volumeday = "VOLUMEDAY"
case volumedayto = "VOLUMEDAYTO"
case volume24Hour = "VOLUME24HOUR"
case volume24Hourto = "VOLUME24HOURTO"
case openday = "OPENDAY"
case highday = "HIGHDAY"
case lowday = "LOWDAY"
case open24Hour = "OPEN24HOUR"
case high24Hour = "HIGH24HOUR"
case low24Hour = "LOW24HOUR"
case lastmarket = "LASTMARKET"
case change24Hour = "CHANGE24HOUR"
case changepct24Hour = "CHANGEPCT24HOUR"
case changeday = "CHANGEDAY"
case changepctday = "CHANGEPCTDAY"
case supply = "SUPPLY"
case mktcap = "MKTCAP"
case totalvolume24H = "TOTALVOLUME24H"
case totalvolume24Hto = "TOTALVOLUME24HTO"
}

init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)

type = try values.decode(String.self, forKey: .type)
market = try values.decode(String.self, forKey: .market)
fromsymbol = try values.decode(String.self, forKey: .fromsymbol)
tosymbol = try values.decode(String.self, forKey: .tosymbol)
flags = try values.decode(String.self, forKey: .flags)

price = try values.decode(Double.self, forKey: .price)
lastvolume = try values.decode(Double.self, forKey: .lastvolume)
lastvolumeto = try values.decode(Double.self, forKey: .lastvolumeto)
lastupdate = try values.decode(Int.self, forKey: .lastupdate)

if let value = try? values.decode(Int.self, forKey: .lasttradeid) {
lasttradeid = String(value)
} else {
lasttradeid = try values.decode(String.self, forKey: .lasttradeid)
}

volumeday = try values.decode(Double.self, forKey: .volumeday)
volumedayto = try values.decode(Double.self, forKey: .volumedayto)
volume24Hour = try values.decode(Double.self, forKey: .volume24Hour)
volume24Hourto = try values.decode(Double.self, forKey: .volume24Hourto)
openday = try values.decode(Double.self, forKey: .openday)
highday = try values.decode(Double.self, forKey: .highday)
lowday = try values.decode(Double.self, forKey: .lowday)
open24Hour = try values.decode(Double.self, forKey: .open24Hour)
high24Hour = try values.decode(Double.self, forKey: .high24Hour)
low24Hour = try values.decode(Double.self, forKey: .low24Hour)

lastmarket = try values.decode(String.self, forKey: .lastmarket)

change24Hour = try values.decode(Double.self, forKey: .change24Hour)
changepct24Hour = try values.decode(Double.self, forKey: .changepct24Hour)
changeday = try values.decode(Double.self, forKey: .changeday)
changepctday = try values.decode(Double.self, forKey: .changepctday)

supply = try values.decode(Double.self, forKey: .supply)
mktcap = try values.decode(Double.self, forKey: .mktcap)
totalvolume24H = try values.decode(Double.self, forKey: .totalvolume24H)
totalvolume24Hto = try values.decode(Double.self, forKey: .totalvolume24Hto)
}
}

成功响应后,我无法解析JSON,我研究了很多关于Swift Codable的嵌套JSON解析,但仍然无法获得成功。

请帮我解析上面带有嵌套 JSON 结构的 JSON 响应,例如说 DisplayRaw 对象具有 Usd 的所有属性。

我认为我犯了一些小错误。

我们将不胜感激。

更新

我已经为响应创建了 JSON 文件并对其进行了解析,

if let path = Bundle.main.path(forResource: "test", ofType: "json") {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .mappedIfSafe)
let result = try JSONDecoder().decode(CoinList.self, from: data)
print(result)
} catch {
print(error.localizedDescription)
}
}

错误是这样的,

enter image description here

JSON结构是这样的,

enter image description here

请找到我的 web api 调用,

    Alamofire.request(TickerRouter.PriceMultiFull(params: params))
.validate(statusCode: 200..<300)
.responseString { responseData in
let data = responseData.value?.replacingOccurrences(of: "\\/", with: "/").data(using: .utf8)
if responseData.error == nil {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .custom({ AnyKey(stringValue: $0.last!.stringValue.lowercased())!})
decoder.dateDecodingStrategy = .secondsSince1970
let result = try? decoder.decode(CoinList.self, from: data!)
success(result!)
} else {
let msg = "Something went wrong. Please try again later"
failure(msg)
}
}

这里的结果为 nil,它正在处理本地 json 文件。 :(

最佳答案

乍一看,您会注意到USD 字典中rawdisplay 的成员类型大不相同,因此单个结构两者都不起作用。

根对象是(String键是BTHXRP符号)

struct CoinList: Codable {
let raw: [String: Raw]
let display: [String: Display]
}

RawDisplay 结构包含 usd 键和适当的结构

struct Raw: Codable {
let usd: USDRaw
}

struct Display: Codable {
let usd: USDDisplay
}

USDRawUSDDisplay 结构包含所有数据,USDRaw 中的lastupdate 将被解码为日期

struct USDRaw: Codable {
let type, market, flags, fromsymbol, tosymbol: String
let price : Double
let lastupdate: Date
let lastvolume, lastvolumeto: Double
let lasttradeid: String
let volumeday, volumedayto, volume24hour, volume24hourto: Double
let openday, highday, lowday, open24hour: Double
let high24hour, low24hour: Double
let lastmarket: String
let change24hour, changepct24hour, changeday, changepctday: Double
let supply, mktcap, totalvolume24h, totalvolume24hto: Double

}

struct USDDisplay: Codable {
let fromsymbol, tosymbol, market, price, lastupdate: String
let lastvolume, lastvolumeto, lasttradeid, volumeday, volumedayto, volume24hour, volume24hourto : String
let openday, highday, lowday, open24hour, high24hour, low24hour, lastmarket: String
let change24hour, changepct24hour, changeday, changepctday: String
let supply, mktcap, totalvolume24h, totalvolume24hto: String
}

要摆脱指定所有 CodingKeys 并使键小写,请创建一个辅助结构(documentation 中窃取)

struct AnyKey: CodingKey {
var stringValue: String
var intValue: Int?

init?(stringValue: String) {
self.stringValue = stringValue
self.intValue = nil
}

init?(intValue: Int) {
self.stringValue = String(intValue)
self.intValue = intValue
}
}

将自定义的 keyDecodingStrategy 和合适的 dateDecodingStrategy 传递给解码器

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .custom({ AnyKey(stringValue: $0.last!.stringValue.lowercased())!})
decoder.dateDecodingStrategy = .secondsSince1970
let coinList = try decoder.decode(CoinList.self, from: data)
print(coinList)

关于ios - JSON 解析,嵌套 JSON 结构问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50829545/

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