ネストされたJSONによる解析でエラーが発生しました(スウィフト5)

パブロPicloso:

私はJSONをデコードしようとしていますが、ネストされた値を持ついくつかの困難を持っています。以下の画像は、私のJSON私はのようなルックスを取得してるものです。

ここでは、画像の説明を入力します。

私の目標は、「価格」の下にある「regularMarketOpen」、下の「生」の値を取得することです。次のような私の構造体のルックス:

struct Response: Codable {
    let symbol: String?
    let price: [Price]?

}

struct Price: Codable {
    let quoteSourceName: String?
    let regularMarketOpen: [regularMarketOpen]?
}

struct regularMarketOpen: Codable {
    let fmt: String?
}

何らかの理由で、私はそうのようなエラーを取得しておいてください。

ERROR: typeMismatch(Swift.Array<Any>, Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "price", intValue: nil)], debugDescription: "Expected to decode Array<Any> but found a dictionary instead.", underlyingError: nil))

これは私の検索方法の問題ですか?ありがとうございました!

Tieda魏:

regularMarketOpenそしてpriceオブジェクトであるように見えます。しかしlet regularMarketOpen: [regularMarketOpen]?、それは配列であることを意味します。

ただ、解析する際に注意すること、{}オブジェクトで、[]配列です。

これを試して:

struct Response: Codable {
    let symbol: String?
    let price: Price?
}

struct Price: Codable {
    let quoteSourceName: String?
    let regularMarketOpen: regularMarketOpen?
}

struct regularMarketOpen: Codable {
    let fmt: String?
}

エラーのこれらの種類を回避するためには、使用することができますhttps://app.quicktype.ioをモデル構造体を生成します。

ハッピー解析:)

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=383744&siteId=1