user3481441
user3481441

Reputation:

populate select dropdown with keys from JSON array

I have the following json-array available:

{
    "Particulier": {
        "Weekend": {
            "2015": {
                "04": [
                    {
                        "startDate": "24-04-2015",
                        "endDate": "27-04-2015",
                        "price": 1111
                    },
                    {
                        "startDate": "15-04-2015",
                        "endDate": "22-04-2015",
                        "price": 9999
                    },
                    {
                        "startDate": "17-04-2015",
                        "endDate": "24-04-2015",
                        "price": 0
                    }
                ],
                "05": [
                    {
                        "startDate": "01-05-2015",
                        "endDate": "04-05-2015",
                        "price": 2222
                    }
                ]
            },
            "2016": {
                "05": [
                    {
                        "startDate": "08-05-2016",
                        "endDate": "15-05-2016",
                        "price": 5555
                    },
                    {
                        "startDate": "15-05-2016",
                        "endDate": "22-05-2016",
                        "price": 6666
                    },
                    {
                        "startDate": "13-05-2016",
                        "endDate": "20-05-2016",
                        "price": 11111
                    }
                ],
                "04": [
                    {
                        "startDate": "24-04-2016",
                        "endDate": "27-04-2016",
                        "price": 1111
                    },
                    {
                        "startDate": "15-04-2016",
                        "endDate": "22-04-2016",
                        "price": 9999
                    },
                    {
                        "startDate": "17-04-2016",
                        "endDate": "24-04-2016",
                        "price": 0
                    }
                ]
            }
        },
        "Midweek": {
            "2015": {
                "04": [
                    {
                        "startDate": "20-04-2015",
                        "endDate": "24-04-2015",
                        "price": 3333
                    },
                    {
                        "startDate": "27-04-2015",
                        "endDate": "01-05-2015",
                        "price": 4444
                    }
                ]
            }
        }
    },
    "Clienten en patienten": {
        "Weekend": {
            "2016": {
                "01": [
                    {
                        "startDate": "08-01-2016",
                        "endDate": "11-01-2016",
                        "price": 7777
                    },
                    {
                        "startDate": "09-01-2016",
                        "endDate": "16-01-2016",
                        "price": 8888
                    }
                ]
            }
        }
    }
}

Is it possible to retrieve all the keys on the same level so they can be used by jQuery to populate a select dropdown.

With on the same level I mean : 'Particulier' & 'Clienten en patienten' and 'Weekend' & 'Midweek' & 'Week' and so on..

If any information is required please ask, new to this. Thanks in advance!

Upvotes: 2

Views: 1589

Answers (3)

Abhishek Mishra
Abhishek Mishra

Reputation: 81

Object.keys(jsonObject) will also help you to get keys and to list.

Upvotes: 1

Anto king
Anto king

Reputation: 1222

HTML Code: <select id="DLState">

JS Part:

    var listItems= "";
$.each( datajson, function( key1, value1 ) {

    $.each( value1, function( key2, value2 ) {


        listItems+= "<option value='" + key2 + "'>" + key2 + "</option>";

    });

});
$("#DLState").html(listItems);

Try something like this demo Demo

Upvotes: 1

user1477388
user1477388

Reputation: 21430

Here is what I think you are trying to do:

http://jsfiddle.net/5wvLapox/

$.each( d, function( key1, value1 ) {
    console.log(value1);
    $.each( value1, function( key2, value2 ) {
       $('#put').append( $('<div></div>').html(key2) );
    });
});

Output:

Weekend 
Midweek

Edit:

Based on your comment, looks like you only want the first level:

http://jsfiddle.net/5wvLapox/2/

$.each( d, function( key1, value1 ) {
    $('#put').append( $('<div></div>').html(key1) );
});

Output:

Particulier
Clienten en patienten

Upvotes: 2

Related Questions