Parsing Json Array Gives Error
I have the following Javascript object: var o = { 'username':'username', 'args': [ '1', '2', '3' ] }; And send it like: xhr.send(JSON.stringify(o));
Solution 1:
Normal JavaScript arrays are designed to hold data with numeric indexes. Try using Object instead of an array.
Try using the below code for constructing the object and check the output :
var o = {}; // Object
o['username'] = 'username';
o['args'] = []; // Array
o['args'].push('1');
o['args'].push('2');
o['args'].push('3');
var json = JSON.stringify(o);
alert(json);
Solution 2:
I think you have one too many quotes in your stringify result. When I construct the object like this:
var o = {
username: "username",
args: ["1","2","3"]
};
the result from calling JSON.stringify(o)
is this
"{\"username\":\"username\",\"args\":[\"1\",\"2\",\"3\"]}"
notice there are no quotes around my square brackets.
Post a Comment for "Parsing Json Array Gives Error"