我在stringyfing一个对象,比如{'foo': 'bar'}
如何将字符串转换回对象?
我在stringyfing一个对象,比如{'foo': 'bar'}
如何将字符串转换回对象?
你需要JSON.parse()有效的JSON字符串。
Var STR = '{"hello":"world"}'; 尝试{ var obj = JSON.parse(str);//这是如何将字符串解析为JSON的 document.body.innerHTML += obj.hello; } catch (ex) { console.error (ex); }
JSON。stringify和JSON。Parse几乎都是对位的,“通常”这样做是可以的:
var obj = ...;
var json = JSON.stringify(obj);
var obj2 = JSON.parse(json);
所以obj和obj2是“相同的”。
然而,也有一些限制需要注意。 在处理简单对象时,这些问题通常并不重要。 但我将在这里用这个helper函数说明其中一些:
function jsonrepack( obj ) { return JSON.parse(JSON.stringify(obj) ); }
You'll only get ownProperties of the object and lose prototypes: var MyClass = function() { this.foo="foo"; } MyClass.prototype = { bar:"bar" } var o = new MyClass(); var oo = jsonrepack(o); console.log(oo.bar); // undefined console.log( oo instanceof MyClass ); // false You'll lose identity: var o = {}; var oo = jsonrepack(o); console.log( o === oo ); // false Functions dont survive: jsonrepack( { f:function(){} } ); // Returns {} Date objects end up as strings: jsonrepack(new Date(1990,2,1)); // Returns '1990-02-01T16:00:00.000Z' Undefined values dont survive: var v = { x:undefined } console.log("x" in v); // true console.log("x" in jsonrepack(v)); // false Objects that provide a toJSON function may not behave correctly. x = { f:"foo", toJSON:function(){ return "EGAD"; } } jsonrepack(x) // Returns 'EGAD'
我相信其他内置类型也有问题。(所有这些都是使用node.js进行测试的,因此根据您的环境,您可能会得到略微不同的行为)。
当问题出现时,有时可以使用JSON的附加参数来解决。parse和JSON.stringify。例如:
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
建议使用JSON.parse
你还可以选择:
var myObject = eval('(' + myJSONtext + ')');
javascript Json
为什么使用JavaScript的eval函数是一个坏主意?
这个怎么样
var parsed = new Function('return ' + stringifiedJSON )();
这是更安全的评估方法。
var stringifiedJSON = '{“hello”:“world”}'; var parsed = new Function('return ' + stringifiedJSON)(); alert(parsed.hello);
看看这个。 http://jsfiddle.net/LD55x/
代码:
var myobj = {};
myobj.name="javascriptisawesome";
myobj.age=25;
myobj.mobile=123456789;
debugger;
var str = JSON.stringify(myobj);
alert(str);
var obj = JSON.parse(str);
alert(obj);
http://jsbin.com/tidob/1/edit?js,console,output
原生JSON对象包括两个关键方法。
1. JSON.parse()
2. JSON.stringify()
JSON.parse()方法解析JSON字符串——即重构原始JavaScript对象 var jsObject = JSON.parse(jsonString); JSON.stringify()方法接受一个JavaScript对象并返回其等效的JSON对象。 var jsonString = JSON.stringify(jsObject);
那么这个部分解呢?
我想存储(使用Config节点)一个全局bigobj,与数据+方法(作为导入外部库的替代方案),在我的流的许多函数节点中使用:
很奇怪,但很有效: 全局变量'bigobj':
{
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版本(都在一行中:()
使用: 在函数节点内部:
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:
还“这里”,不信!
即我必须使用bigobj.....将函数callJSONMethod()添加到任何函数块 也许可以接受。
致以最亲切的问候