struct TranslationJson : Codable{ let results : [Result] let status : String } struct Result : Codable { let elevation : Double struct Location : Codable { let lat : Double let lon : Double } let location : Location let resolution : Double } } let task = URLSession.shared.dataTask(with: request) { data, response, error in if let data = data{ do { let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) print(json) let decoder: JSONDecoder = JSONDecoder() do { let newJson: TranslationJson = try decoder.decode(TranslationJson.self, from: json as! Data) print(newJson) //Success!!! } catch { print("json convert failed in JSONDecoder", error.localizedDescription) } //let json = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments) } catch { print("Serialize Error") } } else { print(error ?? "Error") } } task.resume()
jsonを構造化しjsonの値を取れるようにしたいです。しかし、
色々調べながらjsonの構造化を図ったのですが、うまくいかないです。
{
results = (
{
elevation = "3.06859827041626";
location = {
lat = "34.482902";
lng = "136.82661";
};
resolution = "9.543951988220215";
}
);
status = OK;
}
Could not cast value of type '__NSDictionaryI' (0x101df1508) to 'NSData' (0x101df0090).
2018-06-05 09:00:14.127686+0900 googleAPI[1744:118233] Could not cast value of type '__NSDictionaryI' (0x101df1508) to 'NSData' (0x101df0090).
コンソールに上のように書いてありました。
何がダメなのか教えて欲しいです。
ちなみにjsonの出力はこんな感じです
{ "results" : [ { "elevation" : 1608.637939453125, "location" : { "lat" : 39.73915360, "lng" : -104.98470340 }, "resolution" : 4.771975994110107 } ], "status" : "OK" }


回答1件
あなたの回答
tips
プレビュー