javascript 将嵌套JSON字符串值“\\n”替换为“\n”

dy1byipe  于 2023-06-04  发布在  Java
关注(0)|答案(5)|浏览(480)

我在一些字符串值中嵌套了JSON和,作为字符串\\n的一部分。我需要用\n替换它并返回对象。
我的脑子被洗了,因为我试了一整天,看不到错误。

var data = {
  "d": {
    "name": "Some text",
    "style": [
      {
        "title": {
          "showtitle": true,
          "en": "Need help?",
          "de": "\\nBrauchen Sie\\nHilfe?",
          "font": "Helvetica-Semibold"
        }
      },
      {
        "subtitle": {
          "showtitle": true,
          "en": "English Text",
          "de": "\\nGerman\\nText",
          "font": "Helvetica-Semibold"
        }
      }
    ]
  }
};

      // Replace '\\n' with '\n' in the JSON data
      function update(object) {
          Object.keys(object).forEach(function (k,v) {
              var v = object[k];
              if (object[k] && typeof object[k] === 'object') {
                  return update(object[k])
              }
              if (object[k] === 'en' || object[k] === 'de') {
                  object[k] = v.replace(/\\n/g, '\n');
              }
              console.log('Key: ',k);
              console.log('Value: ',v);
          });
          return object;
      }

      data = update(data);
      console.log('Data: ', data)

为什么不更新值?错误在哪里?

8tntrjer

8tntrjer1#

您需要在此行上测试k,而不是object[k]

// testing for the value
   if (object[k] === 'en' || object[k] === 'de') {
     object[k] = v.replace(/\\n/g, '\n');
   }

   //testing for the key
   if (k === 'en' || k === 'de') {
     object[k] = v.replace(/\\n/g, '\n');
   }
var data = {
  "d": {
    "name": "Some text",
    "style": [{
        "title": {
          "showtitle": true,
          "en": "Need help?",
          "de": "\\nBrauchen Sie\\nHilfe?",
          "font": "Helvetica-Semibold"
        }
      },
      {
        "subtitle": {
          "showtitle": true,
          "en": "English Text",
          "de": "\\nGerman\\nText",
          "font": "Helvetica-Semibold"
        }
      }
    ]
  }
};

// Replace '\\n' with '\n' in the JSON data
function update(object) {
  Object.keys(object).forEach(function(k, v) {
    var v = object[k];
    if (object[k] && typeof object[k] === 'object') {
      return update(object[k])
    }
    if (k === 'en' || k === 'de') {
      object[k] = v.replace(/\\n/g, '\n');
    }
  });
  return object;
}

data = update(data);
console.log('Data: ', data)

另外,不管怎样,这里有一个更精简的函数版本

const update2 = object => {
  for (let o in object) object[o] = typeof object[o] === "string" ? object[o].replace(/\\n/g, '\n') : update2(object[o])
  return object;
}
var data = {
  "d": {
    "name": "Some text",
    "style": [{
        "title": {
          "showtitle": true,
          "en": "Need help?",
          "de": "\\nBrauchen Sie\\nHilfe?",
          "font": "Helvetica-Semibold"
        }
      },
      {
        "subtitle": {
          "showtitle": true,
          "en": "English Text",
          "de": "\\nGerman\\nText",
          "font": "Helvetica-Semibold"
        }
      }
    ]
  }
};
const update2 = object => {
  for (let o in object) object[o] = typeof object[o] === "string" ? object[o].replace(/\\n/g, '\n') : update2(object[o])
  return object;
}

console.log('Data2: ', update2(data))
yhuiod9q

yhuiod9q2#

你的问题是因为if (object[k] === 'en' || object[k] === 'de')应该是if (k === 'en' || k === 'de')

var data = {
  "d": {
    "name": "Some text",
    "style": [
      {
        "title": {
          "showtitle": true,
          "en": "Need help?",
          "de": "\\nBrauchen Sie\\nHilfe?",
          "font": "Helvetica-Semibold"
        }
      },
      {
        "subtitle": {
          "showtitle": true,
          "en": "English Text",
          "de": "\\nGerman\\nText",
          "font": "Helvetica-Semibold"
        }
      }
    ]
  }
};

      // Replace '\\n' with '\n' in the JSON data
      function update(object) {
          Object.keys(object).forEach(function (k,v) {
              var v = object[k];
              if (object[k] && typeof object[k] === 'object') {
                  return update(object[k])
              }
              if (k === 'en' || k === 'de') {
                  object[k] = v.replace(/\\n/g, '\n');
              }
              //console.log('Key: ',k);
              //console.log('Value: ',v);
          });
          return object;
      }

      data = update(data);
      console.log('Data: ', data)
3bygqnnd

3bygqnnd3#

问题在于update函数中的条件if (object[k] === 'en' || object[k] === 'de')。这个条件是检查当前属性值是否等于字符串'en''de',这不是您想要的。
下面是正确的代码:

// Replace '\\n' with '\n' in the JSON data
function update(object) {
  Object.keys(object).forEach(function (k) {
    var v = object[k];
    if (v && typeof v === 'object') {
      return update(v);
    }
    if (k === 'en' || k === 'de') {
      object[k] = v.replace(/\\n/g, '\n');
    }
    console.log('Key: ', k);
    console.log('Value: ', v);
  });
  return object;
}

data = update(data);
console.log('Data: ', data);

现在,当您运行代码时,它将正确地将嵌套JSON字符串值中的\\n替换为\n
希望有帮助

wecizke3

wecizke34#

您可以使用JSON replacer函数在一行中完成转换。当属性名为“de”且值为字符串时,该函数将所有“\n”替换为“\n”。

片段

let data = {"d":{"name":"Some text","style":[{"title":{"showtitle":true,"en":"Need help?","de":"\\nBrauchen Sie\\nHilfe?","font":"Helvetica-Semibold"}},{"subtitle":{"showtitle":true,"en":"English Text","de":"\\nGerman\\nText","font":"Helvetica-Semibold"}}]}}

data = JSON.parse(JSON.stringify(data, (k,v) => 
    k === "de" && typeof v === "string" ? v.replaceAll("\\n", "\n") : v ))

console.log(data);
yqhsw0fo

yqhsw0fo5#

这个可能有用将\n替换为~n(伪字符,并将伪字符替换为\n
`let strv 1 = '德国人?';
let strv2 = strv1.replace('\n','~ n').replace('~',''));”

相关问题