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/

1
  • 7
    values shuld be {} not [] Commented May 19, 2015 at 12:17

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

you can also stringify arrays, what are you talking about?
4

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.