I am having a bear of a time understanding simple JSON serialization principles with Swift 3. Can I please get some help with decoding JSON from a website into an array so I can access it as jsonResult["team1"]["a"]
etc? Here is relevant code:
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()
the incoming JSON is:
{
team1 = {
a = 1;
b = 2;
c = red;
};
team2 = {
a = 1;
b = 2;
c = yellow;
};
team3 = {
a = 1;
b = 2;
c = green;
};
}
Thanks
See Question&Answers more detail:os