JSON stringify returns empty string [duplicate] - javascript

This question already has answers here:
JSON.stringify doesn't work with normal Javascript array
(6 answers)
Closed 7 years ago.
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/

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.

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

Related

Array to json using javascript [duplicate]

This question already has answers here:
JavaScript associative array to JSON
(5 answers)
Closed 4 years ago.
I am trying to pass an array through jQuery's ajax. The problem is that when I try to pass the array created in JavaScript to JSON, it returns something empty. I even try console.log, but when I try to convert it to JSON there is nothing. Here is a representation of how I do it:
var data = [];
data['name'] = 'test';
data['mail'] = 'test';
data['pass'] = 'test';
console.log(JSON.stringify(data)); // result []
Every array is an object. You are assigning object properties with the data['name'] = 'test' syntax. Arrays are indexed with integers and they "must" be in sequence. try a[0] = 'foo'. or Array.push

Get JSON key name [duplicate]

This question already has answers here:
Javascript get Object property Name
(4 answers)
Closed 6 years ago.
I have the following JSON data: {"success":"You are welcome"} that I have named json in my JavaScript code.
When I want to alert You are welcome I do json.success. So now the problem I am facing is that, what about if I want to alert success. Is there any way to get it?
So now the problem I am facing is that, what about if I want to alert
success. Is there a need way to get it ?
If your object is
var obj = {"success":"You are welcome"};
You can get the array of keys as
var keys = Object.keys(obj);
and then print it as
console.log( keys[ 0 ] ); //or console.log( keys.join(",") )
var obj = {"success":"You are welcome"};
var keys = Object.keys(obj);
console.log(keys[0]);
You mean something like this?
keys = Object.keys(json_object)
key_to_use = keys[0];
Try this code
alert(Object.keys({"success":"You are welcome"})[0]);
Since you're able to do json.success, you don't have "JSON data", you have a Javascript Object. JSON, or JavaScript Object Notation, is no more than the serialization of a Javascript object.
As other answers have stated, you can use Object.keys() to list the fields of an object.
Object.keys() can be called on any JavaScript object to get back a list of keys.

Can you create an associative array in JavaScript? [duplicate]

This question already has an answer here:
JSON Stringify Removing Data From Object
(1 answer)
Closed 6 years ago.
I am trying to set up an associative array in JavaScript and then store it in HTML5 Local Storage:
var student = [];
student["david"] = "He is doing very well";
localStorage['student'] = JSON.stringify(student);
var data = JSON.parse(localStorage['student']);
alert(data.david);
There is no such thing as an "associative array" in JavaScript. You are using a regular array which requires the use of numeric indexes, and adding arbitrary properties to it, which are ignored when you serialize the array to JSON.
The JSON-stringified result will be: "[]".
You need to use a real object:
var student = {}

Updating a result in JSON with a dynamic variable name [duplicate]

This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
I need to update a value in my JSON.
My JSON result looks like this:
results = {"ROWCOUNT":50,"COLUMNS":["PERSONID","NAME"],"DATA":{"PERSONID":["42","43","44"], "NAME":["JOE","TOM","JANE"]}
resultData = results.DATA
In the below code I am looping over the result set and attempting to update a value at a position. I believe it is failing because I am not using dynamic variables correctly.
var columnName = "NAME";
for(i=0; i < results.ROWCOUNT; i++ ){
resultData.columnName[i] = "foo" // failing here due to "columnName" being dynamic.
}
Figured it out.. You have to use array syntax
resultData[columName][i]

How to get form data as a object in jquery [duplicate]

This question already has answers here:
Convert form data to JavaScript object with jQuery
(58 answers)
Closed 9 years ago.
I have tried jQuery('#form_id').serialize(). This returns only the form data as a url encoded string. Is it possible to get the form data as an object?
Have you tried "serializeArray"? That gives you an array of names and values. You could turn that into an object if you wanted to:
var paramObj = {};
$.each($('#myForm').serializeArray(), function(_, kv) {
paramObj[kv.name] = kv.value;
});
(I'll have to check again to see what jQuery does with arrays; I think it encodes them as Javascript array values, but I'm not 100% sure.)
edit ah no, it doesn't set up multi-valued parameters as arrays - you get repeats of the same name. Thus, the make-an-object code should look like this:
var paramObj = {};
$.each($('#myForm').serializeArray(), function(_, kv) {
if (paramObj.hasOwnProperty(kv.name)) {
paramObj[kv.name] = $.makeArray(paramObj[kv.name]);
paramObj[kv.name].push(kv.value);
}
else {
paramObj[kv.name] = kv.value;
}
});
(or something like that; could probably be squeezed a little.)
You may take a look at the serializeArray function:
$('#form_id').serializeArray()

Categories