简体   繁体   English

为什么我不使用JSON使用Decodable?

[英]Why i do not get JSON use Decodable?

I try get json but it doesn't work for me. 我尝试获取json,但对我不起作用。 What i'm doing wrong? 我做错了什么? JSON My struct JSON 我的结构

struct Stock: Decodable {
  let category_name: String?
  let privileges: [StockInfo]?
}

struct StockInfo: Decodable {
  let id: String?
  let active_to: String?
  let title: String?
  let title_size: String?
  let preview_picture_path: String?
  let preview_picture_text: String?
  let picture_text_size: String?
  let marked: String?
  let preview_text: String?
  let code_block: [CodeBlockInfo]?    
}

struct CodeBlockInfo: Decodable {
  let button_title: String?
  let button_font_color: String?
  let button_back_color: String?
  let type: String?
  let code_text: String?
  let code_pict: String?
  let description: String?
}

1- CodeBlock is a dictionary not an array 1- CodeBlock是字典而不是数组

let code_block:CodeBlockInfo

2- Decode like this 2-这样解码

let res = try? JSONDecoder().decode([Stock].self,from:jsonData)

3- If you're sure that a key will always return remove the trailing ? 3-如果您确定某个键将始终返回,请删除尾部的?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM