Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON stringify returns empty string [duplicate]

In Javascript I try to use stringify but it keeps returning an empty string. What is wrong here? Feel free to edit the Fiddle.

JS

values = [];
values['belopp'] = 2322;
values['test'] = 'jkee';

str = JSON.stringify(values);

console.log(values);
console.log(str); // Expected to show a json array

JS Fiddle

https://jsfiddle.net/L4t4vtvd/

like image 892
Jens Törnell Avatar asked Dec 11 '22 23:12

Jens Törnell


1 Answers

You are trying to use something that is meant for an object on an array.

values = {};
values['belopp'] = 2322;
values['test'] = 'jkee';

str = JSON.stringify(values);

This is the updated fiddle.

like image 56
vjdhama Avatar answered Dec 20 '22 19:12

vjdhama