我有一個時間理解與Swift 3簡單的JSON序列化原則熊我可以請得到一些幫助解碼JSON從一個網站到一個數組,所以我可以訪問它作爲jsonResult["team1"]["a"]
等?下面是相關的代碼:JSONSerialization與Swift 3
let httprequest = URLSession.shared.dataTask(with: myurl){ (data, response, error) in
self.label.text = "RESULT"
if error != nil {
print(error)
} else {
if let urlContent = data {
do {
let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options:
JSONSerialization.ReadingOptions.mutableContainers)
print(jsonResult) //this part works fine
print(jsonResult["team1"])
} catch {
print("JSON Processing Failed")
}
}
}
}
httprequest.resume()
傳入的JSON是:
{
team1 = {
a = 1;
b = 2;
c = red;
};
team2 = {
a = 1;
b = 2;
c = yellow;
};
team3 = {
a = 1;
b = 2;
c = green;
};
}
感謝