5

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/

Jens Törnell
  • 20,740
  • 40
  • 109
  • 183

2 Answers2

9

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.

vjdhama
  • 4,556
  • 5
  • 32
  • 47
4

You are stringifying an array ([]), not an object ({}) Therefore, values = {};

Kutyel
  • 7,375
  • 2
  • 29
  • 59