如何在iOS Swift中访问Google Tag Manager值集合中的嵌套JSON
我在iOS 8.1/Swift/XCode 6.1中使用GoogleAnalytics iOS SDK 3.10 我有一个带有值集合宏的GTM容器,其中包含嵌套的JSON,如下所示:如何在iOS Swift中访问Google Tag Manager值集合中的嵌套JSON,ios,json,swift,google-tag-manager,Ios,Json,Swift,Google Tag Manager,我在iOS 8.1/Swift/XCode 6.1中使用GoogleAnalytics iOS SDK 3.10 我有一个带有值集合宏的GTM容器,其中包含嵌套的JSON,如下所示: { "cat": { "legs": 4, "sound": "meow" }, "dog": { "legs": 4, "sound": "woof" }, "fox": {
{
"cat":
{
"legs": 4,
"sound": "meow"
},
"dog":
{
"legs": 4,
"sound": "woof"
},
"fox":
{
"legs": 4,
"sound": "?"
}
}
我如何得到狗的腿数?我试过了(除其他变体外)
container.stringForKey(“dog.legs”)
,其中container
是TAGContainer
的一个实例,这不起作用
如果我检查container.stringForKey(“dog”),我会得到一个字符串,“{legs=4,sound=woof}”
。不幸的是,我不能使用.propertyList()
string方法从字符串中获取字典,因为有逗号而不是分号,即使我用分号替换逗号,如果值中有空格,我也会遇到问题。例如,如果我使用了“woof-woof”
而不是“woof”
,则.propertyList()
方法无法从字符串解析字典。它也是无效的JSON字符串,因此我也不能使用JSON解析工具
在Swift中是否有一种干净(或至少推荐)的方法来实现这一点