我需要将json文件中的嵌套数组值添加到swift 4中的字典中。
这是我正在加载的json:
{
"type": "Scripted",
"language": "English",
"genres": [
"Drama",
"Romance"
],
"status": "Ended",}
我正在创建一个结构字典,它将包含来自json文件的数据,但是我在添加" genres"的内容时遇到了问题。在字典里。这就是我初始化的方式:
struct Show{
let id: Int
let name: String
let type: String
let genres: Genres
enum SerializationError:Error{
case missing(String)
case invalid (String, Any)
}
init?(json:[String:Any]) throws{
guard let id = json["id"] as? Int else {throw SerializationError.missing("id is missing")}
guard let name = json["name"] as? String else {throw SerializationError.missing("name is missing")}
guard let type = json["type"] as? String else {throw SerializationError.missing("type is missing")}
guard let genresDict = json["genres"] as? [String: Any] else {throw SerializationError.missing("genres is missing")}
guard let genres = Genres(dict: genresDict) else {return nil}
self.id = id
self.name = name
self.type = type
}
struct Genres{
let genres:String
init?(dict: [String: Any]){
guard let genres = dict[] as? String else { return nil }
self.genres = genres
}
}
}
我不确定如何定义"类型的路径"这个位保护中的数组让genres = dict []为?字符串else {return nil}
我应该如何从那里开始?