gpt4 book ai didi

json - 如何使用响应数据解码值

转载 作者:行者123 更新时间:2023-11-30 10:47:08 26 4
gpt4 key购买 nike

投资组合模型

public struct PortfolioModel : Decodable {

let symbols_requested: Int
let symbols_returned: Int
let data: [Portfolio]
}
struct Portfolio : Decodable {

let stockValue : StockValue
let todaysLowHigh : HighLowValue
let fiftyTwoWeekLowHigh : HighLowValue
let priceBandLowHigh : HighLowValue
let stockPriceValue : StockPriceValue
let stockStatistics : StockStatistics

struct StockValue: Decodable {

let stockName: String?
let stockCurrentPrice: String?
let stockChangeValue : String?
let stockVolume : String?
let stockDateValue : String?
}

struct HighLowValue: Decodable {

let minimumValue : String?
let maximumValue : String?
let currentValue : String?
}

struct StockPriceValue: Decodable {

let bidPriceValue : String?
let bidQtyValue : String?
let offerPriceValue : String?
let offerOtyValue : String?
let openPriceValue : String?
}

struct StockStatistics : Decodable{

let stockMarketCapValue : String?
let stockDividendValue : String?
let stockDivYield : String?
let stockfaceValue :String?
let stockMarketLot : String?
let stockIndustryPE : String?

let stockEPSTTM : StandColidate
let stockPC : StandColidate
let stockPE : StandColidate
let stockPriceBook : StandColidate
let stockBookValue : StandColidate

let stockDeliverables : String?
}

struct StandColidate : Decodable{

let standalone : String?
let consolidate: String?
}
}

Web服务客户端服务器

    .responseJSON { response in
switch response.result {
case .success:
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
do {

let result = try decoder.decode(PortfolioModel.self, from: response.data!)
self.portfolio = result.data
print(result.data)
completion(self.portfolio)

return
}
catch {
MKProgress.hide()
print("Decoding error:", error)
}
case .failure(let error):
MKProgress.hide()
print("Request failed with error: \(error)")
}

服务器响应

{
"symbols_requested": 1,
"symbols_returned": 1,
"data": [
{
"symbol": "AAPL",
"name": "Apple Inc.",
"currency": "USD",
"price": "196.85",
"price_open": "196.45",
"day_high": "197.10",
"day_low": "195.93",
"52_week_high": "233.47",
"52_week_low": "142.00",
"day_change": "1.16",
"change_pct": "0.59",
"close_yesterday": "195.69",
"market_cap": "926316741610",
"volume": "8909408",
"volume_avg": "28596757",
"shares": "4715280000",
"stock_exchange_long": "NASDAQ Stock Exchange",
"stock_exchange_short": "NASDAQ",
"timezone": "EDT",
"timezone_name": "America/New_York",
"gmt_offset": "-14400",
"last_trade_time": "2019-04-05 12:28:19"
}
]
}

我收到 catch block 错误消息

Decoding error: keyNotFound(CodingKeys(stringValue: "stockValue", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "data", intValue: nil), _JSONKey(stringValue: "Index 0", intValue: 0)], debugDescription: "No value associated with key CodingKeys(stringValue: \"stockValue\", intValue: nil) (\"stockValue\").", underlyingError: nil))

我知道它的解码方式是错误的!所有字符串值都放入单个struct Portfolio但是我可以通过以下ProtfolioModel实现它吗?

最佳答案

错误非常明显:键data的数组中没有键stockValue。无论如何,该数组中没有表示结构的字典。

这两个结构与 JSON 匹配,在 quicktype.io 上快速创建

public struct PortfolioModel : Decodable {

let symbolsRequested: Int
let symbolsReturned: Int
let data: [Portfolio]

enum CodingKeys: String, CodingKey {
case symbolsRequested = "symbols_requested"
case symbolsReturned = "symbols_returned"
case data = "data"
}
}

struct Portfolio: Codable {
let symbol, name, currency, price, priceOpen, dayHigh, dayLow: String
let the52_WeekHigh, the52_WeekLow, dayChange, changePct, closeYesterday, marketCap: String
let volume, volumeAvg, shares, stockExchangeLong, stockExchangeShort, timezone: String
let timezoneName, gmtOffset, lastTradeTime: String

enum CodingKeys: String, CodingKey {
case symbol = "symbol", name = "name", currency = "currency"
case price = "price", priceOpen = "price_open", dayHigh = "day_high"
case dayLow = "day_low", the52_WeekHigh = "52_week_high", the52_WeekLow = "52_week_low"
case dayChange = "day_change", changePct = "change_pct", closeYesterday = "close_yesterday"
case marketCap = "market_cap", volume = "volume", volumeAvg = "volume_avg", shares = "shares"
case stockExchangeLong = "stock_exchange_long", stockExchangeShort = "stock_exchange_short"
case timezone = "timezone", timezoneName = "timezone_name", gmtOffset = "gmt_offset", lastTradeTime = "last_trade_time"
}
}

关于json - 如何使用响应数据解码值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55540562/

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