如何替换json字符串中的占位符?

时间:2019-02-26 19:06:15

标签: javascript json

我具有json格式的以下配置:

var obj = {"PUSH" : {"title" : "Medicines Packed - Bill Value    {amount}", "body" : "We have packed your medicines. Your final bill value is Rs {amount} . We will shortly update you on the delivery boy details."}};

var myJSON = JSON.stringify(obj);
myJSON.replace("{amount}", "100");

我基本上有{amount}作为占位符,我正试图以上述方式在Node.js中替换它。但是占位符保持原样,并且不会进行替换。正确的方法是什么?

1 个答案:

答案 0 :(得分:0)

您可以使用split and join

var obj = {
  "PUSH": {
    "title": "Medicines Packed - Bill Value    {amount}",
    "body": "We have packed your medicines. Your final bill value is Rs {amount} . We will shortly update you on the delivery boy details."
  }
};
console.log(JSON.stringify(obj).split("{amount}").join("100"))