Ios 如何将此字典强制转换为数组或访问值?{x=";1.1";y=";2.3";}

Ios 如何将此字典强制转换为数组或访问值?{x=";1.1";y=";2.3";},ios,json,swift,dictionary,nsarray,Ios,Json,Swift,Dictionary,Nsarray,我正在使用api获取一些数据。json在它的一个字段中有这个数据结构 以下是访问我的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

我正在使用api获取一些数据。json在它的一个字段中有这个数据结构

以下是访问我的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()
打印(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
)
],
{
    employees = "1.9";
    others = "2.4";
    pupils = "<null>";
    students = 1;
},
{
    employees = "2.6";
    others = "3.1";
    pupils = "<null>";
    students = "1.9";
}
[[“名称”:通心粉和奶酪,“id”:2915045,“类别”:Tagesgericht 3,“价格”:{
雇员=“2.6”;
其他=“3.1”;
学生=”;
学生=“1.9”;
},“注”:(
Schwefeldioxid和亚硫酸盐,
森夫,
米尔奇和莱克托斯,
魏岑,
格特雷德谷谷酒店,
醇,
麻省理工学院Farbstoff
)
],
打印(价格):

[["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
)
],
{
    employees = "1.9";
    others = "2.4";
    pupils = "<null>";
    students = 1;
},
{
    employees = "2.6";
    others = "3.1";
    pupils = "<null>";
    students = "1.9";
}
{
雇员=“1.9”;
其他=“2.4”;
学生=”;
学生=1;
},
{
雇员=“2.6”;
其他=“3.1”;
学生=”;
学生=“1.9”;
}
我在获取id、类别或注释方面没有问题!唯一的问题是价格

如何访问此数据结构?我想将双值保存到一个数组。

是否尝试使用“SwiftyJSON”CoCoCoapods?如果没有,我建议安装“SwiftyJSON”。然后它就很容易了,如下所示

let json = data

let employees = json["employees"]
let others = json["others"]
let pupils = json["pupils"]
let students = json["students"]

如果您感兴趣,可以通过此链接
如果您不知道cocoapods的使用或安装情况,您可能需要这样做Paulw11在他的评论中给出了答案:

[["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
)
],
{
    employees = "1.9";
    others = "2.4";
    pupils = "<null>";
    students = 1;
},
{
    employees = "2.6";
    others = "3.1";
    pupils = "<null>";
    students = "1.9";
}
我的未知数据结构是json中的一个字典

我更改了以下代码:

if let userId = entry["id"], let price = entry["prices"]
为此:

if let userId = entry["id"], let price = entry["prices"] as? [String:Any]]
现在我可以这样使用它:

var employer = price["employees"]

多亏了Paulw11;)

您的对象中没有
id
prices
元素。您到底想做什么?创建三个数组?这不是有效的JSON(不清楚“打印它”是什么意思).你能提供实际的JSON吗?这看起来很糟糕;
students
是字符串还是双精度的?是
students
字符串还是其他的?Paul我添加了更多信息
prices
是另一个对象(字典),所以如果让pricesDict=entry[“prices”]作为?[string:Any],然后您只需访问
pricesDict[“employees”]
或您显示的任何内容。您发布的第一个JSON显示的内容类似于prices数组中的两个条目。第二个数据类似于更大内容数组的一部分的Xcode转储,其中包括一个“prices”数组中只有一个条目。您还没有给我们完整的图片。将调用中的所有
json
数据发布到JSONSerializationThank,但是必须有一种方法可以使用纯swift而不使用任何LIB?除了第一块json显示包含2个字典的数组,第二块json显示位于e“价格”你还没有解释这些不同的片段是从哪里来的,为什么它们不同。我想有时候你可能会得到一个单独的字典,有时候你会得到一个字典数组,但那很糟糕。最好总是得到一个或多个字典数组。@Duncac Sorr我现在很困惑;)我现在编辑了我的问题,也许现在我已经清楚我做了什么!