-1
在這我需要從兒童數組將獲得在下面提到的網址的元素,我需要從所有11個對象獲得兒童數組任何人都可以幫助我如何實現這個?如何在swift 3中使用json解析子數組?
let url = "http://www.json-generator.com/api/json/get/cwqUAMjKGa?indent=2"
var detailsArray :[[String: AnyObject]] = []
var productName = [String]()
var childrenArray :[[String: AnyObject]] = []
override func viewDidLoad() {
super.viewDidLoad()
downloadJsonWithURL()
// Do any additional setup after loading the view.
}
func downloadJsonWithURL() {
let url = NSURL(string: self.url)
URLSession.shared.dataTask(with: (url as URL?)!, completionHandler: {(data, response, error) -> Void in
if let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? NSDictionary {
self.detailsArray = (jsonObj!.value(forKey: "data") as? [[String: AnyObject]])!
print(self.detailsArray)
for item in self.detailsArray{
if let detailDict = item as? NSDictionary {
if let name = detailDict.value(forKey: "name"){
self.productName.append(name as! String)
}
}
}
OperationQueue.main.addOperation({
print(self.productName)
print(self.childrenArray)
})
}
}).resume()
}
其工作,但只得到了11個對象的孩子數組但我需要讓所有的兒童數組 –
已經更新了我的答案,是的,因爲你已經遍歷數組的循環,所以你必須追加每一個數據,否則它只會顯示你最後的數據。當你在swift中編碼時,請檢查我的更新回答 –
,不要使用NSDictionary和NSArray等所有客觀的c語法語法。使用純粹的快速語法。 只是我的建議 –