var str = '{"hello":"world"}';
try {
var obj = JSON.parse(str); // this is how you parse a string into JSON
document.body.innerHTML += obj.hello;
} catch (ex) {
console.error(ex);
}
I'm sure there are issues with other built-in-types too. (All this was tested using node.js so you may get slightly different behaviour depending on your environment too).
When it does matter it can sometimes be overcome using the additional parameters of JSON.parse and JSON.stringify. For example:
function MyClass (v) {
this.date = new Date(v.year,1,1);
this.name = "an object";
};
MyClass.prototype.dance = function() {console.log("I'm dancing"); }
var o = new MyClass({year:2010});
var s = JSON.stringify(o);
// Smart unpack function
var o2 = JSON.parse( s, function(k,v){
if(k==="") {
var rv = new MyClass(1990,0,0);
rv.date = v.date;
rv.name = v.name;
return rv
} else if(k==="date") {
return new Date( Date.parse(v) );
} else { return v; } } );
console.log(o); // { date: <Mon Feb 01 2010 ...>, name: 'an object' }
console.log(o.constructor); // [Function: MyClass]
o.dance(); // I'm dancing
console.log(o2); // { date: <Mon Feb 01 2010 ...>, name: 'an object' }
console.log(o2.constructor) // [Function: MyClass]
o2.dance(); // I'm dancing
{
some[]more[]{dx:"here"} , // array of objects with array of objects. The 'Config' node requires JSON.
.....
"get_dx": "function( d,p) { return this.some[d].more[p].dx; }" // test function
}
例如,一个函数....的JSON版本(都在一行中:()
< p >使用:
函数节点内部:
var bigO = global.get("bigobj");
function callJSONMethod(obj, fname, a, b, c, d){
// see: https://stackoverflow.com/questions/49125059/how-to-pass-parameters-to-an-eval-based-function-injavascript
var wrap = s => "{ return " + obj[fname] + " };" //return the block having function expression
var func = new Function(wrap(obj[fname]));
return func.call( null ).call( obj, a, b, c, d); //invoke the function using arguments
}
msg.payload =callJSONMethod(bigO, "get_dx", 2, 2);
return msg:
返回“这里”,不信!
< p >。e我必须使用bigobj.....将函数callJSONMethod()添加到任何函数块
也许可以接受。< / p >