如何从整个json输出中删除方括号前后的双引号

时间:2015-07-10 17:10:25

标签: javascript jquery regex json

在下面查找我的json输出

[
    {
        "tname": "yyyy",
        "first_name": "xxx",
        "last_name": "yyyy",
        "pri": "pub",
        "skills": "[{\"description\": \"1 YEAR\", \"Name\": \"Programming\", \"Competence\": \"PHP\"}, {\"description\": \"2 YEAR\", \"Name\": \"Programming\", \"Competence\": \"JAVA\"}]",
        "contact_phone": "875433333333",
        "experience_years": 2,
        "experience": "[{\"description\": \"deve\", \"endYear\": \"2014-07-08\", \"designation\": \"developer\", \"startYear\": \"2013-03-12\", \"companyName\": \"xxx\"}, {\"description\": \"dev\", \"endYear\": \"2015-02-03\", \"designation\": \"developer\", \"startYear\": \"2014-02-09\", \"companyName\": \"yyy\"}]",
        "accomplishments": "[{\"Institution\": \"EVR COLLG\", \"description\": \"EVT\", \"Name\": \"xxx\", \"Year\": \"2013-04-02\"}, {\"Institution\": \"ASV COLLG\", \"description\": \"ASV\", \"Name\": \"YYY\", \"Year\": \"2014-02-02\"}]",
        "contact_email": "xx@gmail.com",
        "education_gist": [
            "SSLC",
            "HSC",
            "UG",
            "PG"
        ],
        "address": " \r\n \r\n \t\t\t\t\t\t\t\t\tno:7, XXX XX, YYYY- 560543\t\r\n \t\t\t\t\t\t\t\t \r\n ",
        "about_me": " \r\n I am xx from XXX, i am a WWW DDD.\r\n \r\n \r\n\r\n ",
        "education": "[{\"description\": \"school first\", \"startYear\": \"2004-02-02\", \"grade\": \"95\", \"collegeName\": \"govt high school\", \"endYear\": \"2004-01-04\", \"qualification\": \"SSLC\"}, {\"description\": \"class first\", \"startYear\": \"2006-01-01\", \"grade\": \"90\", \"collegeName\": \"gov boy hr school\", \"endYear\": \"2006-12-04\", \"qualification\": \"HSC\"}, {\"description\": \"department first\", \"startYear\": \"2012-06-09\", \"grade\": \"94\", \"collegeName\": \"B.Sc\", \"endYear\": \"2015-07-02\", \"qualification\": \"UG\"}, {\"description\": \"GOLD MEDAL\", \"startYear\": \"2015-07-09\", \"grade\": \"9.8\", \"collegeName\": \"MCA\", \"endYear\": \"2015-07-25\", \"qualification\": \"PG\"}]",
        "user_name": "xx",
        "projects": "[{\"Company\": \"XXX\", \"endYear\": \"2015-08-14\", \"Name\": \"XYZ PROJECT\", \"startYear\": \"2015-07-29\", \"description\": \"DEVELOPER\"}, {\"Company\": \"YYYY\", \"endYear\": \"2015-07-31\", \"Name\": \"ABC PROJECT\", \"startYear\": \"2015-07-01\", \"description\": \"DESIGNER\"}]"
    }
]

我想在" [和之后]"之前删除(")双引号方括号

"skills": "[{\"description\": \"1 YEAR\", \"Name\": \"Programming\", \"Competence\": \"PHP\"}, {\"description\": \"2 YEAR\", \"Name\": \"Programming\", \"Competence\": \"JAVA\"}]",

"技能":

  

" [

和\" JAVA \"}

  

]"

我想要使用jquery或javascript的解决方案,不想在PHP中,如果有人知道的话,给我解决方案。提前致谢

2 个答案:

答案 0 :(得分:3)

假设您的数据名为data

data[0].skills = JSON.parse(data[0].skills);
// etc...

如果要将数组内部的数据保存为字符串,则:

data[0].skills = JSON.parse(data[0].skills).map(function(obj) {
    return JSON.stringify(obj);
});

并以编程方式完成所有操作

data.forEach(function(obj){
    for (var key in obj) {
        if (typeof obj.key === "string" && obj.key[1] === "[") {
            obj[key] = JSON.parse(obj[key]).map(function(inArray) {
                return JSON.stringify(inArray);
            });
        }
    }
});

答案 1 :(得分:2)

这是你应该做的,简单的正则表达式替换



jsonData = '"["smt": "smtelse" ]"'; 

jsonData = jsonData.replace(/"\[/g, '[');
jsonData = jsonData.replace(/\]"/g, ']');