如何取消转义和解析对象的嵌套JSON数组?

时间:2018-07-26 11:20:32

标签: javascript json

我应该取消对此类对象的json数组进行解析

 {
    "httpAlertId": 14,
    "httpAlertName": "raja67",
    "httpUrl": "http://example.com",
    "httpRequestType": "GET",
    "httpHeaders": "sadasdsad",
    "httpVarMap": "{\"Sdsd\":\"dss\"}",
    "httpTimeout": 99,
    "httpRetries": 1,
    "httpCredentials": "{\"username\":\"somename\",\"password\":\"something\"}",
    "mappedAlertActionConfigId": 0
}

我期望的是httpCrenentials的值必须成为普通的JSON。

1 个答案:

答案 0 :(得分:0)

var a = {
    "httpAlertId": 14,
    "httpAlertName": "raja67",
    "httpUrl": "http://example.com",
    "httpRequestType": "GET",
    "httpHeaders": "sadasdsad",
    "httpVarMap": "{\"Sdsd\":\"dss\"}",
    "httpTimeout": 99,
    "httpRetries": 1,
    "httpCredentials": "{\"username\":\"somename\",\"password\":\"something\"}",
    "mappedAlertActionConfigId": 0
}

var nJSON = {};
for(key in a){
  var val;
  try{
    nJSON[key] = JSON.parse(a[key]);
  }catch(e){
    nJSON[key] = a[key];
  }
}

console.log(nJSON);