2017-10-16 39 views
1

我試圖解碼一個json模式來表示它的內容在swift對象中。最後,該計劃是生成swift模型文件。但是首先解碼模式。Swift4解碼json模式中的字典

我可以解碼模式中的'屬性'字典就好了。 但是,當我嘗試解碼屬性中的'項'字典時,出現意外問題:

「預計解碼字典,但找到了字符串/數據。

如果我註釋掉行30 & 35以防止項目被解碼,我會獲得成功。 那麼我的JSONSchema.properties結構與我的Property.items結構之間有什麼區別?

我該在哪裏出錯?

我的代碼:

import Foundation 

enum ObjectType: String, Decodable { 
    case ObjectType = "object" 
    case ArrayType = "array" 
    case StringType = "string" 
    case IntegerType = "integer" 
    case NumberType = "number" 
    case BooleanType = "boolean" 
    case NullType = "null" 
} 

struct JSONSchema : Decodable { 
    let schema: String? 
    let id: String? 
    let properties: Dictionary<String, Property>? 

    enum CodingKeys : String, CodingKey { 
     case schema = "$schema" 
     case id 
     case properties 
    } 
} 

struct Property : Decodable { 
    let id: String? 
    let items: Dictionary<String, Item>? 
    let type: ObjectType? 

    enum CodingKeys : String, CodingKey { 
     case id 
     case items 
     case type 
    } 
} 

struct Item : Decodable { 
    let id: String? 
// let properties: Dictionary<String, Property>? 
// let type: ObjectType? 

    enum CodingKeys : String, CodingKey { 
     case id 
//  case properties 
//  case type 
    } 
} 

let jsonString = """ 
{ 
    "$schema": "http://json-schema.org/draft-06/schema#", 
    "definitions": {}, 
    "id": "http://example.com/example.json", 
    "properties": { 
    "cabinStatistics": { 
     "id": "/properties/cabinStatistics", 
     "items": { 
     "id": "/properties/cabinStatistics/items", 
     "properties": { 
      "cabinClass": { 
      "default": "C", 
      "description": "An explanation about the purpose of this instance.", 
      "examples": [ 
       "C", 
       "M" 
      ], 
      "id": "/properties/cabinStatistics/items/properties/cabinClass", 
      "title": "The cabinclass schema.", 
      "type": "string" 
      }, 
      "count": { 
      "default": 29, 
      "description": "An explanation about the purpose of this instance.", 
      "examples": [ 
       "29" 
      ], 
      "id": "/properties/cabinStatistics/items/properties/count", 
      "title": "The count schema.", 
      "type": "integer" 
      }, 
      "specificities": { 
      "id": "/properties/cabinStatistics/items/properties/specificities", 
      "items": { 
       "id": "/properties/cabinStatistics/items/properties/specificities/items", 
       "properties": { 
       "code": { 
        "default": "ACCEPTED", 
        "description": "An explanation about the purpose of this instance.", 
        "examples": [ 
        "ACCEPTED" 
        ], 
        "id": "/properties/cabinStatistics/items/properties/specificities/items/properties/code", 
        "title": "The code schema.", 
        "type": "string" 
       }, 
       "count": { 
        "default": 21, 
        "description": "An explanation about the purpose of this instance.", 
        "examples": [ 
        "21" 
        ], 
        "id": "/properties/cabinStatistics/items/properties/specificities/items/properties/count", 
        "title": "The count schema.", 
        "type": "integer" 
       } 
       }, 
       "type": "object" 
      }, 
      "type": "array" 
      } 
     }, 
     "type": "object" 
     }, 
     "type": "array" 
    }}, 
    "type": "object" 
} 
""" 

if let jsonData = jsonString.data(using: .utf8) { 
    do { 
     let jsonSchema = try JSONDecoder().decode(JSONSchema.self, from: jsonData) 
     print(jsonSchema) 
     print("Success !!") 
    } 
    catch { 
     print(error) 
     print("Bummer ...") 
    } 
} 

回答

1

一個JSON字典直接解碼成結構

你需要寫

struct Property : Decodable { 
    let id: String? 
    let items : Item? 
    let type: ObjectType? 
...