简体   繁体   中英

remove quotes from keys in a json string using jquery

Consider this as my json string,

{"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..

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:

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:

var result = $.parseJSON( obj );

This will convert your JSON string to javascript objects/arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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