我得到了" 16"从我的关键"价值"在tax_ncurrency中。我使用Alamofire处理json,但我不能将它分配给我的标签:productTax
["data": (
{
"tax_n_currency" = {
Value = 16;
Variable = Tax;
};
},
{
"tax_n_currency" = {
Value = USD;
Variable = Currency;
};
}
)]
我的要求。
Alamofire.request(.GET, url!, parameters: ["accesskey":223344666]).responseJSON { (request, response, result) in
let json = result.value as! [String:AnyObject]
print(json)
let data = json["data"] as! [AnyObject]
//print(mm)
let taxnCurrency = data[0]["tax_n_currency"] as! NSDictionary
print(taxnCurrency)
if let tax = taxnCurrency["Value"]?.intValue{
self.productTax.text = "\(tax) %"
print(tax)
}
}
答案 0 :(得分:0)
尝试:
if let tax = data["data"] as? [AnyObject],
tax_n_currency = tax["tax_n_currency"] as? [String:AnyObject],
value = tax_n_currency["Value"] as? Int {
print(value)
}
答案 1 :(得分:0)
我喜欢使用if
语句检查变量是否真的设置,您可以使用print来检查是否在if
语句中获取。如果这不起作用,请提供有关JSON的更多信息。
编辑:将第一个条件更改为Dict数组
Alamofire.request(.GET, url!, parameters: ["accesskey":223344666]).responseJSON { (request, response, result) in
var taxnCurrency: Integer!
let json = result.value as! [String:AnyObject]
let data = json["data"] as! [[String:AnyObjecy]]
if let dict = json[0]["tax_n_currency"] {
if let tax = dict["Value"] as! Integer {
taxnCurrency = tax
}
}