使用jquery从json字符串中的键中删除引号

时间:2021-11-24 06:14:37

Consider this as my json string,

将此视为我的json字符串,

{"Table" : [{"userid" : "11","name" : "KumarP","designation" : "Business Head",
"phone" : "9789234793","email" : "surfingkumar@gmail.com","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:"surfingkumar@gmail.com", role : "Admin",
       empId : "EI003",reportingto : "KumarP"}]}

I am doing so to use it with jlinq..

我这样做是为了与jlinq一起使用..

2 个解决方案

#1


15  

Use Regular Expressions:

使用正则表达式:

var a='{"Table" : [{"userid" : "11","name" : "KumarP","designation" : "Business Head","phone" : "9789234793","email" : "surfingkumar@gmail.com","role" : "Admin",    "empId" : "EI003","reportingto" : "KumarP"}]}';
a=a.replace(/"(\w+)"\s*:/g, '$1:');
alert(a);

The string will become as your second codeblock:

该字符串将成为您的第二个代码块:

{Table: [{userid: "11",name: "KumarP",designation: "Business Head",phone: "9789234793",email: "surfingkumar@gmail.com",role: "Admin",    empId: "EI003",reportingto: "KumarP"}]}

But won't that cause a problem if the label was a reserved word?

但如果标签是保留字,这不会导致问题吗?

#2


6  

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" : "surfingkumar@gmail.com","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对象/数组。

#1


15  

Use Regular Expressions:

使用正则表达式:

var a='{"Table" : [{"userid" : "11","name" : "KumarP","designation" : "Business Head","phone" : "9789234793","email" : "surfingkumar@gmail.com","role" : "Admin",    "empId" : "EI003","reportingto" : "KumarP"}]}';
a=a.replace(/"(\w+)"\s*:/g, '$1:');
alert(a);

The string will become as your second codeblock:

该字符串将成为您的第二个代码块:

{Table: [{userid: "11",name: "KumarP",designation: "Business Head",phone: "9789234793",email: "surfingkumar@gmail.com",role: "Admin",    empId: "EI003",reportingto: "KumarP"}]}

But won't that cause a problem if the label was a reserved word?

但如果标签是保留字,这不会导致问题吗?

#2


6  

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" : "surfingkumar@gmail.com","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对象/数组。