【问题标题】:unable to extract value from struct using swift JSON Decoder无法使用 swift JSON 解码器从结构中提取值
【发布时间】:2018-05-26 13:32:31
【问题描述】:

Click Here for JSON image

请告诉我如何提取“排名”的值。首先检查图像。

我的结构:

struct Result2 : Codable{
    let rankings : [Myrankings3]

    struct Myrankings3 : Codable{
        let ranking : String
    }
}

JSON 代码

do {
    let finalResult = try JSONDecoder().decode(Result2.self, from: data!)
    print(finalResult)        
    // I want to get the value of ranking which is of type string 
}

【问题讨论】:

  • 欢迎来到 Stackoverflow。请不要张贴图片。将 JSON 作为文本发布。
  • 我是新来的。感谢您的建议。但是告诉我如何在给定的结构中获得“排名”的值

标签: json swift swift4 decoder


【解决方案1】:

在根对象(finalResult)中迭代数组rankings

let finalResult = try JSONDecoder().decode(Result2.self, from: data!)
for myRanking in finalResult.rankings {
      print(myRanking.ranking)
}

【讨论】:

  • 请提出一个新问题并添加更多信息,例如您得到的实际错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多