【问题标题】:How to parse JSON without attribute name如何解析没有属性名称的 JSON
【发布时间】:2021-11-24 18:56:04
【问题描述】:
options: [
"6GB RAM/128GB",
"8GB RAM/128GB",
"12GB RAM/256GB"
]

我想用 struct 解析这个 JSON。

我已经编码到这个:

struct Atts: Codable {
    let options: [Options]
    
    enum CodingKeys: String, CodingKey {
        case options
    }
}

struct Options: Codable {

}

但是如何访问“options”数组中的三个元素呢?

感谢您的建议。

【问题讨论】:

  • 只是一个String数组,原样符合Codable。

标签: ios json swift


【解决方案1】:

替换

let options: [Options]

let options: [String]

并删除struct Options: Codable { 在这里使用enum CodingKeys: String, CodingKey { 也是没有意义的,因为您不更改密钥名称所以也将其删除,所以您只需要

struct Atts: Codable {
   let options: [String]
}

【讨论】:

  • 您好,在这种情况下我遇到了问题
  • 如何将这个“选项”分配给一个数组?..就像我之前做过的“var jsondata = [json]()”!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-13
  • 1970-01-01
  • 2020-08-09
  • 2023-03-25
相关资源
最近更新 更多