Y
Y
Yuri2014-11-20 12:35:17
JavaScript
Yuri, 2014-11-20 12:35:17

How to parse multidimensional JSON?

I have a JSON file:

{
    "a":{"x":"y","x1":"y1","x2":"y2"},
    "b":{"z":"c","z1":"c1","z2":"c2"}
}

It is necessary to extract the data x,y and z,c from it. Further parsing one-dimensional JSON using the following construction:
$.getJSON('bt.json', function (data) {
            var items = [];
            $.each(data, function (key, val) {
                items.push('<option id="' + val + '" value="'+val+'">' + key + '</option>');
            });
                    
            $('<select/>', {
                'class': 'my-new-list',
                html: items.join("")
            }).appendTo('.model');

unable to advance. Those. I understand that a string of the form {"x":"y","x1":"y1","x2":"y2"} should get into val, but I can't figure out how to get data from it. Please tell me how this can be done.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nursultan Moldobaev, 2014-11-20
@Diyahon

Probably so

$.getJSON('bt.json', function (data) {
            var items = [];
$.each(data, function (val) {
            $.each(val, function (key, val) {
                items.push('<option id="' + val + '" value="'+val+'">' + key + '</option>');
            });
 });                    
            $('<select/>', {
                'class': 'my-new-list',
                html: items.join("")
            }).appendTo('.model');

R
Rostislav Grigoriev, 2014-11-20
@crazyzubr

val.x, val.x2 etc.

Y
Yuri, 2014-11-20
@Fliss

Thank you, I need to do something like this:

$.getJSON('ko.json', function (data) {
                    var items = [];
                    $.each(data, function (key,val) {
                        $.each(val, function (key1, val1) {
                            if (key=='bt'){items.push('<option id="' + val1 + '" value="' + val1+ '">' + key1 + '</option>');}
                        });
                    });
                    $('<select/>', {
                        'class': 'my-new-list',
                        html: items.join("")
                    }).appendTo('.model');
                })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question