我有一些JSON需要在我的tvOS应用程序中解析和处理。我曾尝试使用网站quicktype.io将JSON转换为结构,但它一无所获。我认为这是因为JSON的布局。
什么是处理这些数据的合适的可编码结构?
{
"bnn":[
"prod",
"staging"
],
"bravo":[
"prod"
],
"comedy":[
"prod"
],
"cp24":[
"prod"
],
"crave":[
"prod",
"french"
],
"ctv":[
"prod",
"prod-empty",
"prod-broken",
"chromecast-enabled",
"chromecast-enabled-is-enabled-empty",
"chromecast-enabled-some-text",
"chromecast-enabled-configpath-wrongurl",
"chromecast-disabled",
"adobe-heartbeat-enabled-false",
"adobe-heartbeat-enabled-true",
"adobe-heartbeat-is-enabled-empty",
"adobe-heartbeat-is-enabled-some-text",
"ads-enabled-live-disable",
"ads-enabled-live-empty",
"ads-enabled-live-enable",
"ads-enabled-live-some-text",
"ads-enabled-vod-disable",
"ads-enabled-vod-empty",
"ads-enabled-vod-enable",
"ads-enabled-vod-some-text",
"chromecast-enabled-applicationid-empty",
"chromecast-enabled-upnext-empty",
"chromecast-enabled-wrong-applicationid",
"chromecast-upnext-disabled",
"chromecast-upnext-enabled",
"chromecast-upnext-isenabled-empty",
"chromecast-upnext-isenabled-some-text",
"chromecast-upnext-namescape-some-text",
"chromecast-upnext-namespace-empty",
"closed-captioning-disabled",
"closed-captioning-enabled",
"closed-captioning-enabled-empty",
"closed-captioning-enabled-some-text",
"comscore-enabled-false",
"comscore-is-enabled-empty",
"comscore-is-enabled-some-text",
"comscore-publisher-id-empty",
"comscore-publisher-id-some-text",
"comscore-secret-empty",
"comscore-secret-some-text",
"conviva-customer-key-empty",
"conviva-customer-key-some-text",
"conviva-enabled-empty",
"conviva-enabled-false",
"conviva-enabled-some-text",
"conviva-enabled-true",
"conviva-product-type-empty",
"conviva-product-type-some-text",
"conviva-url-empty",
"conviva-url-some-text",
"enable-endscreen-disabled",
"enable-endscreen-empty",
"enable-endscreen-some-text",
"enable-episode-list-disabled",
"enable-episode-list-empty",
"enable-episode-list-enabled",
"enable-episode-list-some-text",
"enable-shareable-disabled",
"enable-shareable-empty",
"enable-shareable-enabled",
"enable-shareable-some-text",
"enable-upnext-disabled",
"enable-upnext-empty",
"enable-upnext-enabled",
"enable-upnext-some-text",
"endscreen-enabled",
"krux-brand-empty",
"krux-brand-some-text",
"krux-id-empty",
"krux-id-some-text",
"krux-is-enabled-empty",
"krux-is-enabled-false",
"krux-is-enabled-some-text",
"krux-is-enabled-true",
"krux-type-empty",
"krux-type-some-text",
"language-empty",
"language-enabled-en",
"language-enabled-fr",
"language-some-text",
"location-services-disable",
"location-services-empty",
"location-services-enable",
"location-services-some-text",
"moat-enabled-false",
"moat-enabled-true",
"moat-html5partnercode-missing",
"moat-html5partnercode-some-text",
"moat-is-enabled-empty",
"moat-is-enabled-some-text",
"omniture-channel-empty",
"omniture-channel-some-text",
"omniture-enabled-empty",
"omniture-enabled-false",
"omniture-enabled-some-text",
"omniture-enabled-true",
"omniture-phonetrackingid-empty",
"omniture-phonetrackingid-some-text",
"omniture-server-empty",
"omniture-server-some-text",
"omniture-tablettrackingid-empty",
"omniture-tablettrackingid-some-text",
"omniture-video-player-name-empty",
"omniture-video-player-name-some-text",
"show-cellular-streaming-message-disabled",
"show-cellular-streaming-message-empty",
"show-cellular-streaming-message-enabled",
"show-cellular-streaming-message-some-text",
"upnext-duration-disabled",
"upnext-duration-empty",
"upnext-duration-enabled",
"upnext-duration-some-text",
"upnext-offset-disabled",
"upnext-offset-empty",
"upnext-offset-enabled",
"upnext-offset-some-text",
"video-wifi-or-wifi-and-cell-disabled",
"video-wifi-or-wifi-and-cell-empty",
"video-wifi-or-wifi-and-cell-enabled",
"video-wifi-or-wifi-and-cell-some-text"
],
"ctv_news":[
"prod"
],
"ctvhub":[
"prod"
],
"ctvcomedy":[
"prod"
],
"ctvdrama":[
"prod"
],
"dgo":[
"prod"
],
"e_brand":[
"prod"
],
"rds":[
"prod"
],
"se":[
"prod"
],
"snackable":[
"prod"
],
"space":[
"prod"
],
"tsn":[
"prod"
],
"vidiqa":[
"prod"
],
"vidiqa111":[
"prod",
"omniture-server-empty"
]
}
请告知?
我不打算为您键入整个结构,但首先是
struct MyData: Codable {
let bnn: [String]
let bravo: [String]
let comedy: [String]
let cp24: [String]
let brave: [String]
// and so on until
let vidiqa111: [String]
}
如果这些字符串值是固定的,您可以为它们定义一个枚举:
enum StringValue: String {
case prod
case staging
case french
// ...
}
并将结构的属性更改为CCD_ 1类型。
如果你能在字典/数组中只使用字符串,你也可以简单地使用quicktype.io建议的:
typealias MyData = [String: [String]]
let myData = try? JSONDecoder().decode(MyData.self, from: jsonData)