JSON.stringify() array bizarreness with Prototype.js
Since JSON.stringify has been shipping with some browsers lately, I would suggest using it instead of Prototype’s toJSON. You would then check for window.JSON && window.JSON.stringify and only include the json.org library otherwise (via document.createElement('script')
…). To resolve the incompatibilities, use:
if(window.Prototype) {
delete Object.prototype.toJSON;
delete Array.prototype.toJSON;
delete Hash.prototype.toJSON;
delete String.prototype.toJSON;
}
JSON.stringify doesn't work with normal Javascript array
JavaScript arrays are designed to hold data with numeric indexes. You can add named properties to them because an array is a type of object (and this can be useful when you want to store metadata about an array which holds normal, ordered, numerically indexed data), but that isn't what they are designed for.
The JSON array data type cannot have named keys on an array.
When you pass a JavaScript array to JSON.stringify
the named properties will be ignored.
If you want named properties, use an Object, not an Array.
const test = {}; // Objecttest.a = 'test';test.b = []; // Arraytest.b.push('item');test.b.push('item2');test.b.push('item3');test.b.item4 = "A value"; // Ignored by JSON.stringifyconst json = JSON.stringify(test);console.log(json);
Prototype.js 1.6.x toJSON misbehaves
Use JSON.stringify but with following tweak to get correct output (in case of Arrays):
var _json_stringify = JSON.stringify;
JSON.stringify = function(value) {
var _array_tojson = Array.prototype.toJSON;
delete Array.prototype.toJSON;
var r=_json_stringify(value);
Array.prototype.toJSON = _array_tojson;
return r;
};
This takes care of the Array toJSON incompatibility with JSON.stringify and also retains toJSON functionality as other Prototype libraries may depend on it.
JSON.stringify() array bizarreness with Prototype.js
Since JSON.stringify has been shipping with some browsers lately, I would suggest using it instead of Prototype’s toJSON. You would then check for window.JSON && window.JSON.stringify and only include the json.org library otherwise (via document.createElement('script')
…). To resolve the incompatibilities, use:
if(window.Prototype) {
delete Object.prototype.toJSON;
delete Array.prototype.toJSON;
delete Hash.prototype.toJSON;
delete String.prototype.toJSON;
}
JSON stringify ignoring parameters in different tabs same browser
Looking at the source code of the Zabbix web interface, you can see where the method is overwritten:
zabbix-software$ egrep -iR "JSON.stringify *="
frontends/php/jsLoader.php: 'var _json_stringify = JSON.stringify;'.
frontends/php/jsLoader.php: 'JSON.stringify = function(value) {'.
The original function is still available, just with a different name: _json_stringify()
. The updated jsfiddle is http://jsfiddle.net/u7r8q19g/
update
in Zabbix 5 they put the thing back where it came from or so help me, so now I'm doing:
if (typeof _json_stringify === "function") {
item.html(_json_stringify(JSON.parse(text), undefined, 4))
} else {
item.html(JSON.stringify(JSON.parse(text), undefined, 4))
}
thx to How to tell if a JavaScript function is defined
Problem of serializing javascript object to JSON string
Are you using Prototype? This question may be related:
JSON.stringify() array bizarreness with Prototype.js
JSON parse - stringify - parse broken?
Works for me (FF3.5)
var str = '{"elements":[{"text": "", "colour": "#66AA50", "type": "line"}]}';
var obj = JSON.parse(str);
var str2 = JSON.stringify(obj);
var obj2 = JSON.parse(str2);
Equals = function(a,b)
{
//Check if the arrays are undefined/null
if(!a || !b)
return false;
//first compare their lengths
if(a.length == b.length)
{
//go thru all the vars
for(var i = 0; i < a.length;i++)
{
//if the var is an array, we need to make a recursive check
//otherwise we'll just compare the values
if(typeof a[i] == 'object') {
if(!Equals(a[i],b[i]))
return false;
}
else if(a[i] != b[i])
return false;
}
return true;
}
else return false;
}
alert (Equals (obj,obj2)); //true
alert (JSON.stringify(obj) == JSON.stringify(obj2)); //true
alert (obj == obj2); //false (different pointer)
Issue with JSON.stringify adding a extra \ and to my Json object
It looks like you are placing a string as the value in your map. You should do something like:
var objMap = {"JObject" : ValuesArray};
var json = JSON.stringify(objMap)
What's happening is you are double json encoding your values array - note that your "invalid" JSON value is actually a JSON string rather than the array that you want.
EDIT
It looks like you are sticking in JSON strings of maps into an Array and then stringifying that. Here's a jsfiddle that should help you get what you are looking for - http://jsfiddle.net/4G5nF/
In your post request, try this
var jObject = {"JObject" : ValuesArray};
$.ajax({ url: address,
type: 'POST',
dataType: 'json',
data: jObject,
success: function (data) { .. }});
Note the change in the data attribute. That is a value that is automatically JSONified for you.
Related Topics
Problem in Redirecting Programmatically to a Route in React Router V6
How to Async Await in React Render Function
Javascript: Class.Method VS. Class.Prototype.Method
How Can Jquery Deferred Be Used
Get First and Last Date of Current Month with JavaScript or Jquery
Making Custom Right-Click Context Menus for My Web-App
Remove Not Alphanumeric Characters from String
Coordinating Parallel Execution in Node.Js
Sorting in JavaScript: Should Every Compare Function Have a "Return 0" Statement
Js:Convert Array of Strings to Array of Objects
How to Overload Functions in JavaScript
Swap Rows with Columns (Transposition) of a Matrix in JavaScript
Relation Between Commonjs, Amd and Requirejs
What Is the Cleanest Way to Get the Progress of Jquery Ajax Request