遍历JSON对象 [英] Iterating over a JSON-Object

查看:113
本文介绍了遍历JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下JSON文件:

I have the following JSON file:

{"Mensaplan": [{
        "Montag": [
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": true, "bio": false},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false}
        ],

        "Dienstag": [
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": true},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false}
        ],

        "Mittwoch": [
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": true, "bio": false},
            {"food": "Schnitzl", "price": "4.00 €", "vegetarian": false, "bio": true}
        ],

        "Donnerstag": [
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false}
        ],

        "Freitag": [
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false},
            {"food": "Schnitzl", "price": "5.00 €", "vegetarian": false, "bio": false}
        ]
    }]
}

我想遍历"Mensaplan"并获得每一天("Montag","Dienstag",[...](德语)).我试图用jQuery方法$ .each来做到这一点,但是由于每个人都有不同的名字,所以我不知道如何用通配符来表达这些日子.

I want to iterate over the "Mensaplan" and get each day ("Montag", "Dienstag", [...] (it's German)). I was trying to do this with the jQuery Method $.each but I don't get how to formulate a wildcard for the days as each one has a different name.

有人可以帮我解决这个问题吗?

Can anyone help me with this matter?

提前谢谢!

推荐答案

无需jQuery,简单的

No need for jQuery, a simple for...in loop will work.

var obj = JSON.parse(yourJsonString);

//for each object in the "Mensaplan" array
for(var i = 0; i < obj.Mensaplan.length; ++i) {

    //for each key in the object
    for(var key in obj.Mensaplan[i]) {

        var day = obj.Mensaplan[i][key];

        //here key is the day's name, and day is the data...

    }

}

希望这会有所帮助.

这篇关于遍历JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆