0

我正在尝试\用两个反斜杠替换一个反斜杠,\\但是我的双引号不起作用..

var json = {
    "DateToday": "2021-08-11",
    "MetaData": [
        {
            "id": "222",            
            "nameUsed": " \"data\" somemorefillerdata» - somemorefillerdata «somemorefillerdata»",
            "type": "movies"
        }
    ]
}

let newJson = JSON.stringify(json)
let newnewJson = newJson.replace(/\\"/g, "\\\\");
let newnewnewJson = JSON.parse(newnewJson)
console.log(newnewnewJson)

所以这种工作但是我的输出不包括它看起来像这样的引号 -

{
  DateToday: '2021-08-11',
  MetaData: [
    {
      id: '222',
      nameUsed: ' \\data\\ somemorefillerdata» - somemorefillerdata «somemorefillerdata»',
      type: 'movies'
    }
  ]
}
4

2 回答 2

0

If you need to add one more \ then replace this line:

let newnewJson = newJson.replace(/\\"/g, `\\\\\\\\`);

As I understand from the question, you try to get this output:

" \\data\\ somemorefillerdata» - somemorefillerdata «somemorefillerdata»"

before it was:

" \data\ somemorefillerdata» - somemorefillerdata «somemorefillerdata»"
于 2021-08-25T18:33:09.547 回答
0

I don't see the use for replacing \ with \\. Here the starting point is a object literal. It is then stringifyed, returning the expected result. And then parsed into an object again, returning the expected result.

var jsonObj = {
  DateToday: "2021-08-11",
  MetaData: [{
    id: 222,
    nameUsed: " \"data\" somemorefillerdata» - somemorefillerdata «somemorefillerdata»",
    type: "movies"
  }]
};

let jsonStr = JSON.stringify(jsonObj)
console.log(jsonStr);

let newJsonObj = JSON.parse(jsonStr)
console.log(newJsonObj.MetaData[0].nameUsed)

于 2021-08-25T18:33:16.640 回答