2017-12-22 18 views
1

私はテーブルビューを設定するために使用するJsonデータを持っています。 Jsonはメイン部分に1つの2つのタイプのオブジェクトを持っています。これは "Main"とn-numberのtableviewオブジェクトです。異なるタイプのJsonオブジェクトをデコードしてください

メインオブジェクトには2つの文字列型( "url"と "bar_name")があります。 Tableviewオブジェクトには3つの型( "name"、 "id"、[tableData])があります。これは、すべてのテーブルビューオブジェクト(n個の数値)に共通です。セルを挿入するTabledataオブジェクトは、URLを開き、別のテーブルが新しいテーブルを開き、最後のテーブルがアクションを実行するため、3種類のタイプがあります。

このタイプのjsonを構造体にデコードしたいとします。出来ますか?

//For All Types 
protocol commonCellTypes: Codable{ 
    var type: Int { get } 

} 
struct User: Codable{ 
    let mainPage: MainPage? 
    let tables: [table]? 
} 
struct MainPage: Codable{ 
    let url: String? 
    let bar_name: String? 
} 
struct table: Codable{ 
    let name: String? 
    let id: String? 
    let Cells: [commonCellTypes] 

    enum CodeKeys: CodingKey 
    { 
     case name 
     case id 
     case tableDatas 
    } 

    init(from decoder: Decoder) throws { 
     let container = try decoder.container(keyedBy: CodeKeys.self) 
     name = try container.decode (String.self, forKey: .name) 
     id = try container.decode (String.self, forKey: .id) 
     Cells = try container.decode ([commonCellTypes].self, forKey: .tableDatas) 
    } 

    func encode(to encoder: Encoder) throws 
    { 
     var container = encoder.container(keyedBy: CodeKeys.self) 
     try container.encode (name, forKey: .name) 
     try container.encode (id, forKey: .id) 
     try container.encode (Cells, forKey: .tableDatas) 
    } 
} 
//Type 2 
struct cellType2: commonCellTypes{ 
    let cellText: String 
    let imageName: String? 
    let url: URL? 
    let type: Int 
} 
//Type 1 
struct cellType1: commonCellTypes{ 
    let cellText: String 
    let imageName: String 
    let table_id: String 
    let type: Int 
} 
//Type 0 
struct cellType0: commonCellTypes{ 
    let cellText: String 
    let imageName: String 
    let type: Int 
    let action: String 
} 

enter image description here

編集:私は、同様のポストを見つけましたが、私の状況では動作しません。私は、エラーの下に

「テーブル」は、プロトコルに準拠していないタイプの「エンコード可能な」

Swift JSONDecoder with multiple structures

EDIT2を取得しています:私は、復号化のためのコードを追加しましたが、私は両方のためにnilを取得していますメインページとユーザー。私の構造で何が欠けていますか?

let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped) 
      let decoder = JSONDecoder() 
      let admin = try! decoder.decode(User.self, from: data) 
      print(admin) 
+0

'User'は辞書を含み、' Table1'は辞書も含み、 'Cells'のみが配列を含む。 **出力を注意深く**読んでください。 JSONは** **コレクションタイプのみを取得しました。それらを区別するのは非常に簡単です。 ** '[]' **は配列です** '{}'は辞書です – vadian

+0

@vadian私の質問を編集します。それを確認していただけますか?どうもありがとうございます。 –

+0

もう一度** JSON **を読んでください。あなたは構造を理解することを学ばなければなりません。キー 'テーブル 'はなく、値は配列ではなく**配列です。大文字と小文字の区別が重要! 'User'は' MainPage'、 'Table1'と' Table2'を持っています – vadian

答えて

0

デコード用のカスタムイニシャライザとエンコード用のencode(to encoder: Encoder)を実装する必要があります。

//For All Types 
protocol commonCellTypes: Codable{ 

} 
struct MainStruct: Codable{ 
    let tables: [table] 
} 
struct table: Codable{ 
    let name: String? 
    let id: String? 
    let tableDatas: [commonCellTypes] 

    enum CodeKeys: CodingKey 
    { 
    case name 
    case id 
    case tableDatas 
    } 

    init(from decoder: Decoder) throws { 
     let container = try decoder.container(keyedBy: CodeKeys.self) 
     name = try container.decode (String.self, forKey: .name) 
     id = try container.decode (String.self, forKey: .id) 
     tableDatas = try container.decode ([commonCellTypes].self, forKey: .tableDatas) 
    } 

    func encode(to encoder: Encoder) throws 
    { 
     var container = encoder.container(keyedBy: CodeKeys.self) 
     try container.encode (name, forKey: .name) 
     try container.encode (id, forKey: .id) 
     try container.encode (tableDatas, forKey: .tableDatas) 
    } 
} 
//Type 2 
struct cellType2: commonCellTypes{ 
    let cellText: String 
    let imageName: String? 
    let url: URL? 
    let type: Int 
} 
//Type 1 
struct cellType1: commonCellTypes{ 
    let cellText: String 
    let imageName: String 
    let table_id: String 
    let type: Int 
} 
//Type 0 
struct cellType0: commonCellTypes{ 
    let cellText: String 
    let imageName: String 
    let type: Int 
    let action: String 
} 
+0

Adminのためにstructを作成する必要がありますか?これはjsonの最高レベルですか? –

+0

Btw、私はテーブルの配列ですが、jsonではtable-1、table-2と呼ばれていますが、どのようにデコードのために変更することができますか? –

+0

@ EmreO。はい、JSON応答のすべてのツリーを維持する場合は –

関連する問題