6

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}
        ]
    }]
}

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?

Thank in advance!

Felix
  • 565
  • 1
  • 6
  • 17
  • There is no need for jQuery here, a normal for and/or for-in loop would be sufficient. – Chad Aug 22 '13 at 15:53
  • Schnitzl gnam **var sting='("'+Object.keys(json).join('","')+'")'** if you don't have prototypes. – cocco Aug 22 '13 at 15:59

4 Answers4

7

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...

    }

}

Hope this helps.

Chad
  • 19,219
  • 4
  • 50
  • 73
  • you need to use `hasOwnProperty` here. – Orlando Aug 22 '13 at 16:08
  • @Orlando You do not *need* to use `hasOwnProperty`, only if a library messes with the Object prototype (and if they do you should be aware of it) do you need to use `hasOwnProperty`. And since 2010, if anyone is messing with the Object prototype, they should be using `Object.defineProperty` and be marking as `enumerable: false`. – Chad Aug 22 '13 at 16:20
  • ye you are right, you don't have to use it here, but it's a good practice to always use it, the problem is when you iterate objects that inherits from others – Orlando Aug 22 '13 at 16:37
  • @Orlando I disagree, you should understand when it should be used, and use it appropriately. I don't think it is best practice to throw it into every loop even when it doesn't need to be there. – Chad Aug 22 '13 at 16:42
  • @chad 2 things.. 1. it is a good practice, is ok if you don't want to use it.. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in), if you use jslint it reports that an if to check for own properties is missing. – Orlando Aug 22 '13 at 17:07
  • @Orlando You said "it is good practice to *always* use it" that I disagree with, you should use it when it is appropriate. Just like your MDN link says "If you only want to consider properties attached to the object itself, and not its prototypes" that is a case where you would use it. – Chad Aug 22 '13 at 17:14
3

first parse it using JSON

var json = JSON.parse(jsonString)

then is just a javascript object.. you should use..

Object.keys(json).forEach(function (key) {
    json[key];  
});

if you use for in you need to check that the object has that property and is not one of their parents (if (json.hasOwnProperty) { //code here })

using Object.keys you don't need to do that, since grabs only the keys the object owns.

Orlando
  • 9,374
  • 3
  • 56
  • 53
1
var mensaplan = json['Mensaplan'];
for (key in mensaplan) {
     values = mensaplan[key];
     //do something with the values
}
Madison May
  • 2,723
  • 3
  • 22
  • 32
0

You don't need jquery here, a simple for in is enough, but you have to check the hasOwnProperty function because the for in loop can also retrieve methods of the object.

for (var key in Mensaplan) {
    if (Mensaplan.hasOwnProperty(key) {
        console.log(Mensaplan[key]);
    }
}

UPDATE : in your case Mensaplan is an array contained in a json... for the array the fastest way is a standard for loop, not a for in

for (var i = 0, length = Mensaplan.length; i < length; i++) {
    console.log(Mensaplan[i]);
}
Sebastien C.
  • 2,099
  • 17
  • 21