json
1{ 2 "data": [ 3 { 4 "id": 54770, 5 "title": aaa, 6 }, 7 { 8 "id": 54770, 9 "title": aaa, 10 } 11 ], 12 "_metadata": {} 13}
CompanyResponse.swift
1struct CompanyResponse: Codable { 2 let id: String 3 let title: String 4}
CompanyRequest.swift
1/// 一部のみ 2func response(from object: Any, urlResponse: HTTPURLResponse) throws -> [CompanyResponse] { 3 guard let dictionary = object as? Dictionary<String, Any> else { 4 throw ResponseError.unexpectedObject(object) 5 } 6 let data = dictionary["data"] 7 return try JSONDecoder().decode([CompanyResponse].self, from: JSONEncoder().encode(data)) 8}
↑みたいなjsonのdata以下をapikitで取得したく、apikitのところで“data”以下のオブジェトをdata定数に代入、JSONDecoderでresponseとして変換みたいにやりたかったのですが、変換のところでうまくいってないっぽいです。
そもそも今回のようなjsonはどのようにapikit, codableを使って取得するのが正しいやり方なのでしょうか?
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。