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.
精彩评论