代码之家  ›  专栏  ›  技术社区  ›  ACP

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

  •  6
  • ACP  · 技术社区  · 14 年前

    把它当作我的json字符串,

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

    我想要这样的线,

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

    我这样做是为了和jlinq一起使用。

    2 回复  |  直到 11 年前
        1
  •  18
  •   aularon    11 年前

    使用正则表达式:

    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);
    

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

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

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

        2
  •  7
  •   user113716    14 年前

    如果您所拥有的实际上是一个JSON字符串,如:

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

    然后你可以用 $.parseJSON() ,如:

    var result = $.parseJSON( obj );
    

    这将把JSON字符串转换为JavaScript对象/数组。