如何在Swift 5中打印此JSON值?
如何打印出JSON中的“underlineSymbol”或任何相关字段?我知道打印出一个JSON字段,但如何进入嵌入式字段 因此,我按照以下建议制作了可解码的:如何在Swift 5中打印此JSON值?,json,swift,Json,Swift,如何打印出JSON中的“underlineSymbol”或任何相关字段?我知道打印出一个JSON字段,但如何进入嵌入式字段 因此,我按照以下建议制作了可解码的: { "optionChain": { "result": [ { "underlyingSymbol": "AAPL", "expirationDates": [
{
"optionChain": {
"result": [
{
"underlyingSymbol": "AAPL",
"expirationDates": [
1606435200,
1607040000,
1607644800,
1608249600,
1608768000,
1609372800,
1610668800,
1613692800,
1616112000,
1618531200,
1623974400,
1626393600,
1631836800,
1642723200,
1655424000,
1663286400,
1674172800
],
"strikes": [
55,
60,
65,
70,
75
],
"hasMiniOptions": false,
但是,现在我收到一个错误:
JSONSerialization错误:dataCorrupted(Swift.DecodingError.Context(codingPath:[],debugDescription:“给定的数据不是有效的JSON.”,underyingError:可选(错误域=NSCOCAerorDomain代码=3840“字符1周围的对象中没有字符串键。”用户信息={NSDebugDescription=字符1周围的对象中没有字符串键。})))我会使用可编码的API:
struct Something: Decodable
{
let optionChain: OptionChain
let error: String
}
struct OptionChain: Decodable
{
let result: [ResultElement]
}
struct ResultElement: Decodable
{
let underlyingSymbol: String
let expirationDates: [Int]
let strikes: [Int]
let hasMiniOptions: Bool
let quote: [quoteElement]
let options: [optionsElement]
}
struct quoteElement: Decodable
{
let language: String
let region: String
let quoteType: String
let quoteSourceName: String
let triggerable: Bool
let currency: String
let firstTradeDateMilliseconds: Int
let priceHint: Int
let regularMarketChange: Int
let regularMarketChangePercent: Int
let regularMarketTime: Int
let regularMarketPrice: Int
let regularMarketDayHigh: Int
let regularMarketDayRange: String
let regularMarketDayLow: Int
let regularMarketVolume: Int
let regularMarketPreviousClose: Int
let bid: Int
let ask: Int
let bidSize: Int
let askSize: Int
let fullExchangeName: String
let financialCurrency: String
let regularMarketOpen: Int
let averageDailyVolume3Month: Int
let averageDailyVolume10Day: Int
let fiftyTwoWeekLowChange: Int
let fiftyTwoWeekLowChangePercent: Int
let fiftyTwoWeekRange: String
let fiftyTwoWeekHighChange: Int
let fiftyTwoWeekHighChangePercent: Int
let fiftyTwoWeekLow: Int
let fiftyTwoWeekHigh: Int
let dividendDate: Int
let earningsTimestamp: Int
let earningsTimestampStart: Int
let earningsTimestampEnd: Int
let trailingAnnualDividendRate: Int
let trailingPE: Int
let trailingAnnualDividendYield: Int
let epsTrailingTwelveMonths: Int
let epsForward: Int
let epsCurrentYear: Int
let priceEpsCurrentYear: Int
let sharesOutstanding: Int
let bookValue: Int
let fiftyDayAverage: Int
let fiftyDayAverageChange: Int
let fiftyDayAverageChangePercent: Int
let twoHundredDayAverage: Int
let twoHundredDayAverageChange: Int
let twoHundredDayAverageChangePercent: Int
let marketCap: Int
let forwardPE: Int
let priceToBook: Int
let sourceInterval: Int
let exchangeDataDelayedBy: Int
let tradeable: Bool
let exchange: String
let shortName: String
let longName: String
let marketState: String
let messageBoardId: String
let exchangeTimezoneName: String
let exchangeTimezoneShortName: String
let gmtOffSetMilliseconds: Int
let market: String
let esgPopulated: Bool
let displayName: String
let symbol: String
}
struct optionsElement: Decodable
{
let expirationDate: Int
let hasMiniOptions: Bool
let calls: [callPutElement]
let puts: [callPutElement]
}
struct callPutElement: Decodable
{
let contractSymbol: String
let strike: Int
let currency: String
let lastPrice: Int
let change: Int
let percentChange: Int
let volume: Int
let openInterest: Int
let bid: Int
let ask: Int
let contractSize: String
let expiration: Int
let lastTradeDate: Int
let impliedVolatility: Int
let inTheMoney: Bool
}
使用
JSONDecoder
或JSONSerialization
解析JSON(如何做到这一点是这里最常见的问题之一)。。数组第一项([]
)中keyunderyingsymbol
的值,在字典({}
)中keyresult
)中key的值,在keyoptionChain
中,我理解JSON序列化,但我不理解的是JSON的键。我知道你在那里做了什么。然而,当我尝试这个时,我得到了这个错误。JSONSerialization错误:dataCorrupted(Swift.DecodingError.Context(codingPath:[],debugDescription:“给定的数据不是有效的JSON.”,underyingError:可选(error Domain=NSCocoaErrorDomain Code=3840“字符1周围的对象中没有字符串键。”UserInfo={NSDebugDescription=字符1周围的对象中没有字符串键。}))
struct Something: Decodable {
let optionChain: OptionChain
}
struct OptionChain: Decodable {
let result: [ResultElement]
}
struct ResultElement: Decodable {
let underlyingSymbol: String
let expirationDates: [Int]
let strikes: [Int]
let hasMiniOptions: Bool
}
let jsonString = "{...}"
let jsonData = jsonString.data(using: .utf8)!
let decoder = JSONDecoder()
let something = try decoder.decode(Something.self, from: jsonData)
print(something.optionChain.result.map { $0.underlyingSymbol })