Tôi có một thời gian hiểu các nguyên tắc tuần tự hóa JSON đơn giản với Swift 3. Tôi có thể xin trợ giúp giải mã JSON từ một trang web thành một mảng để tôi có thể truy cập nó như là jsonResult["team1"]["a"]
không? Đây là mã có liên quan:JSONSerialization với 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 đến là:
{
team1 = {
a = 1;
b = 2;
c = red;
};
team2 = {
a = 1;
b = 2;
c = yellow;
};
team3 = {
a = 1;
b = 2;
c = green;
};
}
Cảm ơn