简体   繁体   English

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

[英]remove quotes from keys in a json string using jquery

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一起使用..

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? 但如果标签是保留字,这不会导致问题吗?

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对象/数组。

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

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