我有以下JSon從服務器中檢索到的數據,我想提取值名稱並將它們作爲模型存儲在數組或字典中。問題是,返回自己的價值是以另一種字典的形式出現的。我如何提取頻率,描述和數量的值,並將它們更新到我的tableview。以下是我從服務器請求獲得的Json數據格式。我是新來的迅速和字典的概念和陣列是安靜的困惑,我從Swift iOS9中提取Json數據的值
{
"payment" = (
{
amount = 100;
currency = USD;
description = "Awesome Videos";
frequency = Day;
},
{
amount = 500;
currency = USD;
description = "Awesome Videos";
frequency = Week;
},
{
amount = 3000;
currency = USD;
description = "Awesome Videos";
frequency = Months;
}
);
}
我想他們在本地存儲在字典或數組,並更新到我的tableview。
這裏也是我的代碼從服務器
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { (data, response, error) -> Void in
if (data != nil){
print(data)
do {
// let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers)
let jsonResult = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)
print(jsonResult)
print(jsonResult["payment_plans"])
if let plan_list = jsonResult as? NSDictionary{
print("got dictionary")
for (key, value) in plan_list
{
print(value)
print("printing keys")
print(key)
if let plans = value as? NSDictionary
{
print("printing values")
print(plans)
print(plans["currency"])
}
}
}
} catch{
print("Json Serialization failed")
}
}
}
/*
if (response != nil){
print(response)
}
else{
print(error)
}
}
*/
task.resume()
}
我堅持在這裏如何提取,我在字典中獲取值獲取數據。在此先感謝
我設法得到Json數據....任何想法如何我可以填充數據到tableview?似乎不會在我身邊工作。謝謝 – Arnold
在你的班級 –
中創建一個屬性**付款**對不起,然後把所有數據都放到** payments **屬性中,並在表視圖中調用reloadData()dataSource方法,你可以使用'if let payment = self.payments如? NSDictionary {cell.label.text =付款[「描述」]爲?字符串}'不要忘記你也有一個整數! –