I am using an api to get some data. The json has this data structure in one of its fields.
Here is the code to get access to my json:
let myUrl = NSURL(string:"http://openmensa.org/api/v2/canteens/\(choosenID)/days/" + currentDate + "/meals")!
URLSession.shared.dataTask(with: myUrl as URL) { (data, response, error) in
if error != nil {
print(error!)
} else {
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [[String:Any]] {
print(json)
for entry in json {
if let userId = entry["id"], let name = entry["name"], let category = entry["category"], let price = entry["prices"], let notes = entry["notes"] {
var meal = MealObject(id:userId as! Int, name:name as! String as! String, category:category as! String, price:0.0, notes: notes as! [String]);
print(price)
// DO MY OTHER STUFF...
}
}
} else {
print("JSON is not an array of dictionaries")
}
} catch let error as NSError {
print(error)
}
}
DispatchQueue.main.async {
self.tableView.reloadData()
}
}.resume()
print(json):
[["name": Macaroni & Cheese, "id": 2915045, "category": Tagesgericht 3, "prices": {
employees = "2.6";
others = "3.1";
pupils = "<null>";
students = "1.9";
}, "notes": <__NSArrayI 0x600000298380>(
Schwefeldioxid und Sulfite,
Senf,
Milch und Laktose,
Weizen,
Glutenhaltiges Getreide,
Alkohol,
mit Farbstoff
)
],
print(price):
{
employees = "1.9";
others = "2.4";
pupils = "<null>";
students = 1;
},
{
employees = "2.6";
others = "3.1";
pupils = "<null>";
students = "1.9";
}
I have no problem to get access to the id, the category, or the notes! The only problem are the prices.
How can I get access to this data structure? I want to save the double values to an array.