使用 jquery 从 json 字符串中的键中删除引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3651294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove quotes from keys in a json string using jquery
提问by ACP
Consider this as my json string,
将此视为我的 json 字符串,
{"Table" : [{"userid" : "11","name" : "KumarP","designation" : "Business Head",
"phone" : "9789234793","email" : "[email protected]","role" : "Admin",
"empId" : "EI003","reportingto" : "KumarP"}]}
and i want to have my string like this,
我想要这样的字符串,
{Table:[{ userid: "11", name: "KumarP", designation: "Business Head",
phone: "9789234793", email:"[email protected]", role : "Admin",
empId : "EI003",reportingto : "KumarP"}]}
I am doing so to use it with jlinq..
我这样做是为了将它与 jlinq 一起使用..
回答by aularon
Use Regular Expressions:
使用正则表达式:
var a='{"Table" : [{"userid" : "11","name" : "KumarP","designation" : "Business Head","phone" : "9789234793","email" : "[email protected]","role" : "Admin", "empId" : "EI003","reportingto" : "KumarP"}]}';
a=a.replace(/"(\w+)"\s*:/g, ':');
alert(a);
The string will become as your second codeblock:
该字符串将成为您的第二个代码块:
{Table: [{userid: "11",name: "KumarP",designation: "Business Head",phone: "9789234793",email: "[email protected]",role: "Admin", empId: "EI003",reportingto: "KumarP"}]}
But won't that cause a problem if the label was a reserved word?
但是,如果标签是保留字,那会不会引起问题?
回答by user113716
If what you have is actually a JSON string, as in:
如果您拥有的实际上是一个 JSON 字符串,例如:
var obj = '{"Table" : [{"userid" : "11","name" :"KumarP","designation" : "Business Head",\
"phone" : "9789234793","email" : "[email protected]","role" : "Admin",\
"empId" : "EI003","reportingto" : "KumarP"}]}';
Then you could parse it with $.parseJSON()
, as in:
然后你可以用 解析它$.parseJSON()
,如下所示:
var result = $.parseJSON( obj );
This will convert your JSON string to javascript objects/arrays.
这会将您的 JSON 字符串转换为 javascript 对象/数组。