N
N
Nikolay Baranenko2017-06-05 08:21:01
JavaScript
Nikolay Baranenko, 2017-06-05 08:21:01

Problem in JSON or its parsing method?

Hello.
via Ajax I get JSON like
[{"Employee":"leading specialist","map_list":{"Direction":"New office","DIS":"0"}},{"Employee":"senior specialist", "map_list":{"Direction":"Old office","DIS":"1"}},{"Employee":"boss","map_list":{"Direction":"Virtual office","DIS" :"0"}},{"Employee":"unit_constant","map_list":{"Count":"2","POSTS":"3","NEW":"1"}}]
trying to parse by method

var result = [];
                var keys = Object.keys(jsondata);
                keys.forEach(function(key){
                    result.push(json[key]);
                });

I get the error
Uncaught ReferenceError: json is not defined
Problem in JSON or its parsing method?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yevgeniy Kisselyov, 2017-06-05
@drno-reg

Explicitly says json variable does not exist

var result = [];
                var keys = Object.keys(jsondata);
                keys.forEach(function(key){
                    result.push(jsondata[key]);
                });

N
Nikolay Baranenko, 2017-06-05
@drno-reg

// конвертируем JSON Object в массив
    function JSONtoArray(JSONObject) {
        var result = [];
        for(var k in JSONObject) {
            var v = JSONObject[k];
            result.push(k, v);
        }
        return result;
    }

        $.getJSON( "employees?region="+encodeURI(selected), { selected_region: selected } )
            .done(function( jsondata ) {
var result=JSONtoArray(jsondata[3].map_list);
// далее работаем с массивом
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question