简体   繁体   English

javascript JSON 解析初始化值

[英]javascript JSON parse initialize value

I want to parse json to js object with data.我想用数据将 json 解析为 js 对象。 this is my simple example :这是我的简单示例:

var IsTrue = true;
var Number = 9;
var Object = { a : 1};
var Array = [10,6];

var jStr = '{"ask" : IsTrue, "value" : Number, "obj" : Object, "arr" : Array}';
var js = JSON.parse(jStr);
console.log(js);

I work in this jsfiddle : http://jsfiddle.net/n4tLgp5k/1/我在这个 jsfiddle 工作: http : //jsfiddle.net/n4tLgp5k/1/

I get error :我得到错误:

Uncaught SyntaxError: Unexpected token I未捕获的语法错误:意外的令牌 I

i wish i can parse json to js object and variable in object initialized value with initialized variable before.我希望我之前可以将 json 解析为 js 对象和对象初始化值中的变量,并使用初始化变量。

You have reference to variable inside the string so you can't parse it using JSON.parse() .您引用了字符串中的变量,因此无法使用JSON.parse()解析它。 Convert the string to valid JSON, you can do it using JSON.stringify() .将字符串转换为有效的 JSON,您可以使用JSON.stringify()

 var IsTrue = true; var Number = 9; var Object = { a: 1 }; var Array = [10, 6]; var jStr = '{"ask" : ' + JSON.stringify(IsTrue) + ', "value" :' + JSON.stringify(Number) + ', "obj" : ' + JSON.stringify(Object) + ', "arr" : ' + JSON.stringify(Array) + '}'; var js = JSON.parse(jStr); console.log(js);


If you just need to create the object then you can use object literals and if need json string you can use JSON.stringify()如果您只需要创建对象,那么您可以使用对象文字,如果需要 json 字符串,您可以使用JSON.stringify()

 var IsTrue = true; var Number = 9; var Object = { a: 1 }; var Array = [10, 6]; var js = { ask: IsTrue, value: Number, obj: Object, arr: Array }; console.log(js);


Also there is baddest way using eval() but I will not prefer this method .还有使用eval()最糟糕的方法,但我不会更喜欢这种方法。

Why is using the JavaScript eval function a bad idea? 为什么使用 JavaScript eval 函数是个坏主意?

 var IsTrue = true; var Number = 9; var Object = { a: 1 }; var Array = [10, 6]; var jStr = '{"ask" : IsTrue, "value" : Number, "obj" : Object, "arr" : Array}'; var js = eval('(' + jStr + ')'); console.log(js);

You need a pure string before parsing.在解析之前你需要一个纯字符串。

var IsTrue = true,
    Number = 9,
    Object = { a : 1},
    Array = [10,6];

var jStr = {"ask" : IsTrue, "value" : Number, "obj" : Object, "arr" : Array};
var js = JSON.parse(JSON.stringify(jStr));
console.log(js);

But I am not sure why you want to do this.但我不确定你为什么要这样做。

Unless you have a specific reason for using a string, why not use an object literal like this?除非您有使用字符串的特定原因,否则为什么不使用这样的对象字面量呢?

var IsTrue = true;
var Number = 9;
var Object = { a : 1};
var Array = [10,6];

var js = {
    "ask" : IsTrue, 
    "value" : Number, 
    "obj" : Object, 
    "arr" : Array
};
console.log(js);

Although you should avoid using types as variable names.尽管您应该避免使用类型作为变量名。 Number, Object, Array are all terrible variable names. Number、Object、Array 都是糟糕的变量名。 If it doesn't cause a parse error it's certainly going to confuse someone sometime.如果它不会导致解析错误,它肯定会在某个时候混淆某些人。 And that someone might be your future self.那个人可能是你未来的自己。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM