简体   繁体   English

使用Codable解码具有多个键的JSON

[英]Decode JSON with multiple keys using Codable

I have stored in iCloud several JSON files as a Byte type. 我在iCloud中存储了多个JSON文件作为字节类型。 Hope that's correct so far. 希望到目前为止是正确的。

I've got to fetch those CKRecord s and then parse them and show a graph using the values stored in the JSON. 我必须获取这些CKRecord ,然后解析它们并使用JSON中存储的值显示一个图。 I am capable of fetching the data from iCloud but I got no luck parsing this JSON. 我能够从iCloud中获取数据,但解析此JSON却没有运气。

{"00:00": 17, "00:10": 16, "00:20": 17, "00:30": 16, "00:40": 16, "00:50": 17, "01:00": 16, "01:10": 16, "01:20": 16, "01:30": 16, "01:40": 17, "01:50": 17, "02:00": 18, "02:10": 18, "02:20": 17, "02:30": 17, "02:40": 17, "02:50": 17, "03:00": 16, "03:10": 17, "03:20": 16, "03:30": 16, "03:40": 17, "03:50": 16, "04:00": 16, "04:10": 16, "04:20": 16, "04:30": 16, "04:40": 16, "04:50": 18, "05:00": 17, "05:10": 16, "05:20": 17, "05:30": 17, "05:40": 17, "05:50": 17, "06:00": 17, "06:10": 17, "06:20": 16, "06:30": 16, "06:40": 17, "06:50": 15, "07:00": 15, "07:10": 15, "07:20": 14, "07:30": 15, "07:40": 13, "07:50": 11, "08:00": 8, "08:10": 8, "08:20": 7, "08:30": 5, "08:40": 4, "08:50": 4, "09:00": 2, "09:10": 2, "09:20": 9, "09:30": 8, "09:40": 7, "09:50": 7, "10:00": 5, "10:10": 4, "10:20": 6, "10:30": 5, "10:40": 4, "10:50": 4, "11:00": 3, "11:10": 2, "11:20": 2, "11:30": 1, "11:40": 2, "11:50": 1, "12:00": 1, "12:10": 2, "12:20": 3, "12:30": 3, "12:40": 2, "12:50": 3, "13:00": 1, "13:10": 0, "13:20": 1, "13:30": 0, "13:40": 3, "13:50": 2, "14:00": 3, "14:10": 4, "14:20": 3, "14:30": 6, "14:40": 4, "14:50": 6, "15:00": 5, "15:10": 7, "15:20": 7, "15:30": 7, "15:40": 7, "15:50": 6, "16:00": 7, "16:10": 8, "16:20": 8, "16:30": 6, "16:40": 5, "16:50": 5, "17:00": 5, "17:10": 4, "17:20": 4, "17:30": 6, "17:40": 6, "17:50": 5, "18:00": 6, "18:10": 7, "18:20": 4, "18:30": 3, "18:40": 3, "18:50": 5, "19:00": 5, "19:10": 3, "19:20": 4, "19:30": 4, "19:40": 2, "19:50": 4, "20:00": 1, "20:10": 2, "20:20": 2, "20:30": 1, "20:40": 3, "20:50": 2, "21:00": 4, "21:10": 4, "21:20": 7, "21:30": 7, "21:40": 6, "21:50": 7, "22:00": 8, "22:10": 7, "22:20": 8, "22:30": 9, "22:40": 10, "22:50": 10, "23:00": 10, "23:10": 9, "23:20": 9, "23:30": 10, "23:40": 10, "23:50": 10}

I previously used Codable to parse a JSON using Swift but this case is different. 我以前使用Codable使用Swift解析JSON,但是这种情况有所不同。 I got a lot of keys that are numbers and I don't really know how to approach this. 我有很多数字钥匙,但我真的不知道该如何处理。

你可以试试

let res = try? JSONDecoder().decode([String:Int].self,from:data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM