Hello. There is a string derived from JSON serialization.

[{"input_name": "\u0427\u0443\u0436\u043e\u0439: \u0417\u0430\u0432\u0435\u0442", "input_slug": "alien"}, {"input_name": "\u0412\u0430\u043b\u0435\u0440\u0438\u0430\u043d \u0438 \u0433\u043e\u0440\u043e\u0434 \u0442\u044b\u0441\u044f\u0447\u0438 \u043f\u043b\u0430\u043d\u0435\u0442", "input_slug": "valerian"}, {"input_name": "\u041b\u0435\u043e\u043d", "input_slug": "leon"}, {"input_name": "\u041c\u0435\u0447 \u043a\u043e\u0440\u043e\u043b\u044f \u0410\u0440\u0442\u0443\u0440\u0430", "input_slug": "king-arthur-legend-of-the-sword"}] 

How can javascript convert this string to an array of objects? Using the method below, I create an array with only one element, this is with the same string.

 var xr = JSON.parse("[" + x + "]") 

Thank you in advance :)

    2 answers 2

     var stroka = '[{"input_name": "\u0427\u0443\u0436\u043e\u0439: \u0417\u0430\u0432\u0435\u0442", "input_slug": "alien"}, {"input_name": "\u0412\u0430\u043b\u0435\u0440\u0438\u0430\u043d \u0438 \u0433\u043e\u0440\u043e\u0434 \u0442\u044b\u0441\u044f\u0447\u0438 \u043f\u043b\u0430\u043d\u0435\u0442", "input_slug": "valerian"}, {"input_name": "\u041b\u0435\u043e\u043d", "input_slug": "leon"}, {"input_name": "\u041c\u0435\u0447 \u043a\u043e\u0440\u043e\u043b\u044f \u0410\u0440\u0442\u0443\u0440\u0430", "input_slug": "king-arthur-legend-of-the-sword"}]'; var obj = JSON.parse(stroka); console.log(obj[2]); console.log(obj[3].input_name); u0440 \ u0438 \ u0430 \ u043d \ u0438 \ u0433 \ u043e \ u0440 \ u043e \ u0434 \ u0442 \ u044b \ u0441 \ u044f \ u0447 \ u0438 \ u043f \ u043b \ u0430 \ u043d \ u0435 \ var stroka = '[{"input_name": "\u0427\u0443\u0436\u043e\u0439: \u0417\u0430\u0432\u0435\u0442", "input_slug": "alien"}, {"input_name": "\u0412\u0430\u043b\u0435\u0440\u0438\u0430\u043d \u0438 \u0433\u043e\u0440\u043e\u0434 \u0442\u044b\u0441\u044f\u0447\u0438 \u043f\u043b\u0430\u043d\u0435\u0442", "input_slug": "valerian"}, {"input_name": "\u041b\u0435\u043e\u043d", "input_slug": "leon"}, {"input_name": "\u041c\u0435\u0447 \u043a\u043e\u0440\u043e\u043b\u044f \u0410\u0440\u0442\u0443\u0440\u0430", "input_slug": "king-arthur-legend-of-the-sword"}]'; var obj = JSON.parse(stroka); console.log(obj[2]); console.log(obj[3].input_name); 

    obj is an array of 4 elements, each of which has the properties input_name and input_slug .

      Use

       var arr = JSON.stringify([{"input_name": "\u0427\u0443\u0436\u043e\u0439: \u0417\u0430\u0432\u0435\u0442", "input_slug": "alien"}, {"input_name": "\u0412\u0430\u043b\u0435\u0440\u0438\u0430\u043d \u0438 \u0433\u043e\u0440\u043e\u0434 \u0442\u044b\u0441\u044f\u0447\u0438 \u043f\u043b\u0430\u043d\u0435\u0442", "input_slug": "valerian"}, {"input_name": "\u041b\u0435\u043e\u043d", "input_slug": "leon"}, {"input_name": "\u041c\u0435\u0447 \u043a\u043e\u0440\u043e\u043b\u044f \u0410\u0440\u0442\u0443\u0440\u0430", "input_slug": "king-arthur-legend-of-the-sword"}]); console.log(arr); 

      • I tried, but the result is almost the same. For example, referring to any element of the array, one character is output. And I would like to display a separate object. - Artem Nester
      • So you have an array of objects there, output according to the array index the object you need, what's the problem? - Shnur