我正在尝试用null或其他一些字符替换\“,并尝试JSON解析,但它不会发生在al。下面是我的代码。
var s = '{"total":2,"data":[{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{}],"assignName":"BC Simple Form","assignId":"100003","assignProcessId":"3"},{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{"Name":"Rishant","City":"Bangalore","Desc":"Platform-Dev","AUDIT_COMPANIES":"[{\"AUDIT_COMPANY\":\"Golder\",\"AUDITORS\":[{\"TPA_AUDITOR\":\"Swadhin Ray\",\"IS_LEAD\":\"0\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"},{\"TPA_AUDITOR\":\"Praveen Kayetha\",\"IS_LEAD\":\"1\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"}]}]"}],"assignName":"BC Simple Form","assignId":"100001","assignProcessId":"2"}]}';
s= s.replace(/\\"/g, "DOUBLEQ");
console.log(s);
var data = JSON.parse(s);
console.log(data);
我找到的解决方案是
var dataResponse = JSON.parse(s);
var dataLen = dataResponse.data.length;
for(var i=0;i<dataLen;i++){
var audit_co = dataResponse.data[i].assignAdditionalInfo[0].AUDIT_COMPANIES;
if(typeof audit_co!= 'undefined')
dataResponse.data[i].assignAdditionalInfo[0].AUDIT_COMPANIES = JSON.parse(audit_co.replace(/\\"/g,'"'));
}
答案 0 :(得分:1)
我不认为这是非法的JSON - 它看起来像JSON中的JSON,这是有效的。尝试解析它而不改变它。
答案 1 :(得分:1)
您无需将其解析为无效的JSON:
您可以直接访问值:
<script>
var s = {"total":2,"data":[{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{}],"assignName":"BC Simple Form","assignId":"100003","assignProcessId":"3"},{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{"Name":"Rishant","City":"Bangalore","Desc":"Platform-Dev","AUDIT_COMPANIES":"[{\"AUDIT_COMPANY\":\"Golder\",\"AUDITORS\":[{\"TPA_AUDITOR\":\"Swadhin Ray\",\"IS_LEAD\":\"0\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"},{\"TPA_AUDITOR\":\"Praveen Kayetha\",\"IS_LEAD\":\"1\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"}]}]"}],"assignName":"BC Simple Form","assignId":"100001","assignProcessId":"2"}]};
console.log(s.data);
</script>
答案 2 :(得分:1)
目前你有这个:
var s = '{"plainValue": 42, "nestedJSON": "{\"nested\": \"json\"}"}';
这是无效的,因为它转换为以下内存中的字符串:
{"plainValue": 42, "nestedJSON": "{"nested": "json"}"} ^ ^ ^ ^ invalid double quotes
解决方案很简单:不要使用嵌套的JSON。 没有任何理由将JSON字符串嵌套到JSON字符串中。这是你应该拥有的。
var s = '{"plainValue": 42, "nestedObject": {"nested": "object"}}';
语法上有效但愚蠢的是
var s = '{"plainValue": 42, "nestedJSON": "{\\"nested\\": \\"json\\"}"}';
在源头修复问题。生成JSON字符串的函数已损坏。
答案 3 :(得分:0)
当你创建's'字符串时,\“已经被转义,所以\ char消失。这就是为什么你不能用某些东西替换\”,因为它不存在。
答案 4 :(得分:0)
以下方法应该起作用:
var s = JSON.stringify({"total":2,"data":[{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{}],"assignName":"BC Simple Form","assignId":"100003","assignProcessId":"3"},{"assignExist":"Online","status":"Started Work","state":"Ready for Download","assignInfoletId":"100003","loadAssignId":"false","assignAdditionalInfo":[{"Name":"Rishant","City":"Bangalore","Desc":"Platform-Dev","AUDIT_COMPANIES":"[{\"AUDIT_COMPANY\":\"Golder\",\"AUDITORS\":[{\"TPA_AUDITOR\":\"Swadhin Ray\",\"IS_LEAD\":\"0\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"},{\"TPA_AUDITOR\":\"Praveen Kayetha\",\"IS_LEAD\":\"1\",\"ASSIGNMENTS\":\"Assigmens, Sdsfds,sdfdsfd\"}]}]"}],"assignName":"BC Simple Form","assignId":"100001","assignProcessId":"2"}]});
var data = JSON.parse(s);