2017-09-21 5 views
0

Swift 4のコード可能な機能をjsonで使用したいが、一部のキーにはセット名がありません。むしろ配列があり、Swift 4 JSONコードをキーとして使用

{ 
"status": "ok", 
"messages": { 
    "generalMessages": [], 
    "recordMessages": [] 
}, 
"foundRows": 2515989, 
"data": { 
    "181": { 
    "animalID": "181", 
    "animalName": "Sophie", 
    "animalBreed": "Domestic Short Hair/Domestic Short Hair/Mixed (short coat)", 
    "animalGeneralAge": "Adult", 
    "animalSex": "Female", 
    "animalPrimaryBreed": "Domestic Short Hair", 
    "animalUpdatedDate": "6/26/2015 2:00 PM", 
    "animalOrgID": "12", 
    "animalLocationDistance": "" 

のように、181のIDが表示されます。誰も181を扱う方法を知っているので、私はそれをキーとして指定できますか?番号は任意の数にすることができ、それぞれの番号が異なります。

は、事前にこの

struct messages: Codable { 
    var generalMessages: [String] 
    var recordMessages: [String] 
} 

struct data: Codable { 
    var 
} 

struct Cat: Codable { 
    var Status: String 
    var messages: messages 
    var foundRows: Int 
    //var 181: [data] //What do I place here 
} 

おかげのような何かをしたいと思います。

+0

'181'はそこにある文字列です(' "" 'で囲まれています)。したがって、あなたは 'element [" data "] [181]'ではなく要素 '" data "[" 181 "]'にアクセスします(二重引用符に注意してください)。それはあなたが求めていることですか? –

+0

お返事ありがとうございます。私は自分自身をより明確にしようとするために質問を更新しました。 –

答えて

2

確認してください:私はこのような何かを示唆している

struct ResponseData: Codable { 
    struct Inner : Codable { 
     var animalID : String 
     var animalName : String 

     private enum CodingKeys : String, CodingKey { 
      case animalID  = "animalID" 
      case animalName = "animalName" 
     } 
    } 

    var Status: String 
    var foundRows: Int 
    var data : [String: Inner] 

    private enum CodingKeys: String, CodingKey { 
     case Status = "status" 
     case foundRows = "foundRows" 
     case data = "data" 
    } 
} 

let json = """ 
    { 
     "status": "ok", 
     "messages": { 
      "generalMessages": ["dsfsdf"], 
      "recordMessages": ["sdfsdf"] 
     }, 
     "foundRows": 2515989, 
     "data": { 
      "181": { 
       "animalID": "181", 
       "animalName": "Sophie" 
      }, 
      "182": { 
       "animalID": "182", 
       "animalName": "Sophie" 
      } 
     } 
    } 
""" 
let data = json.data(using: .utf8)! 
let decoder = JSONDecoder() 

do { 
    let jsonData = try decoder.decode(ResponseData.self, from: data) 
    for (key, value) in jsonData.data { 
     print(key) 
     print(value.animalID) 
     print(value.animalName) 
    } 
} 
catch { 
    print("error:\(error)") 
} 
0

数値を変数名として宣言することはできません。 Apple's docから:

定数と変数名は空白文字、 数学記号、矢印、私的使用(または無効)Unicodeコード ポイント、またはラインおよびボックス描画文字を含めることはできません。番号は で始まるものではありませんが、数字は名前の他の場所に含まれることもあります。

適切な方法は、keyをキャプチャするプロパティを持つことです。

struct Cat: Codable { 
    var Status: String 
    var messages: messages 
    var foundRows: Int 
    var key: Int // your key, e.g., 181 
} 
+0

あなたは正しいですが、私はそこに配置する必要があるものを理解しようとしています。 –

+0

変数を使用してキーを保存するだけです – Lawliet

0

: -

struct ResponseData: Codable { 
struct AnimalData: Codable { 
    var animalId: String 
    var animalName: String 

    private enum CodingKeys: String, CodingKey { 
     case animalId = "animalID" 
     case animalName = "animalName" 
    } 
} 

var status: String 
var foundRows: Int 
var data: [AnimalData] 

private enum CodingKeys: String, CodingKey { 
    case status = "status" 
    case foundRows = "foundRows" 
    case data = "data" 
} 

struct AnimalIdKey: CodingKey { 
    var stringValue: String 
    init?(stringValue: String) { 
     self.stringValue = stringValue 
    } 
    var intValue: Int? { return nil } 
    init?(intValue: Int) { return nil } 
} 

init(from decoder: Decoder) throws { 
    let container = try decoder.container(keyedBy: CodingKeys.self) 
    let animalsData = try container.nestedContainer(keyedBy: AnimalIdKey.self, forKey: .data) 

    self.foundRows = try container.decode(Int.self, forKey: .foundRows) 
    self.status = try container.decode(String.self, forKey: .status) 
    self.data = [] 
    for key in animalsData.allKeys { 
     print(key) 
     let animalData = try animalsData.decode(AnimalData.self, forKey: key) 
     self.data.append(animalData) 
    } 
    } 
} 

let string = "{\"status\":\"ok\",\"messages\":{\"generalMessages\":[],\"recordMessages\":[]},\"foundRows\":2515989,\"data\":{\"181\":{\"animalID\":\"181\",\"animalName\":\"Sophie\"}}}" 
let jsonData = string.data(using: .utf8)! 
let decoder = JSONDecoder() 
let parsedData = try? decoder.decode(ResponseData.self, from: jsonData) 

この方法で、あなたのデコーダの初期化自体がキーを処理します。

関連する問題