我有一個數組中Xcode
返回其輸出如下:如何從此JSON數組中抽取此特定節點?
["userDetails": {
id = 31;
"user_email" = "[email protected]";
"user_name" = "Steve Downs";
}, "communities": <__NSArrayI 0x600000245f40>(
{
id = 5;
name = South;
},
{
id = 13;
name = HurraHarry;
},
{
id = 15;
name = EnclliffeT;
}
)
]
下面的代碼正確地分配載「社區」到它們各自的變量中的值。
let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String:AnyObject]
print (json!)
if let arr = json?["communities"] as? [[String:String]] {
self.communitiesArray = arr.flatMap { $0["name"]!}
self.communityIdsArray = arr.flatMap { $0["id"]!}
}
if let arr = json?["userDetails"] as? [AnyObject] {
self.playerId = (arr.flatMap { $0["id"]!} as (AnyObject)) as! [String]
print (self.playerId);
}
但是我無法從"userDetails"
分配id
。如何從"userDetails"
獲取id
並將其分配給self.playerId?
謝謝,我得到:「字符串」不能指定類型的值鍵入'[String]' – RDowns
我知道它的工作原理:self.playerId = [dict [「id」]!] – RDowns