我正在使用Swift 3。
我从服务器收到以下响应,我需要解析并获取诸如帐号,帐号类型,地址等值。
parsedData: (
{
key = "<null>";
offset = 1;
partition = 0;
topic = test;
value = {
"Account No" = 675;
"Account Type" = Saving;
Address = location;
User ID = 601;
User Name = Stella;
};
}
)
我一直在努力获得价值,然后计划获得每个价值,
var temp: NSArray = parsedData["value"] as! NSArray
但这是错误cannot convert value of type String to expected argument type Int.'
如何从上面提到的数组中检索值?
答案 0 :(得分:1)
parsedData
是array
,在第一个索引处包含Dictionary
。
let dicData = parsedData[0] as! Dictionary
let valueDictionary = dicData["value"] as! Dictionary //dicData also contains dictionary for key `value`
let accountNumber = valueDictionary ["Account No"] //**Account number**
//////第二种方法,如果你有反应中的字典
var valueDictionary : NSDictionary = parsedData["value"] as? NSDictionary
let accountNumber = valueDictionary ["Account No"] //**Account number**
答案 1 :(得分:0)
您将字典解析为数组
var temp: NSDictionary = parsedData["value"] as? NSDictionary
答案 2 :(得分:0)
试试这个
let dicData = parsedData[0] as! Dictionary
var temp: NSDictionary = dicData["value"] as? NSDictionary
let accountNumber = temp.object(forKey: "Account No")
let accountType = temp.object(forKey: "Account Type")
let address = temp.object(forKey: "Address")
let userID = temp.object(forKey: "User ID")
let userName = temp.object(forKey: "User Name")