如何从 JSON 键中删除句点?

How do I remove a period from a JSON key?

我有一个 JSON 有效负载,如下所示:

{
"Realtime Currency Exchange Rate": {
    "1. From_Currency Code": "BTC",
    "2. From_Currency Name": "Bitcoin",
    "3. To_Currency Code": "CNY",
    "4. To_Currency Name": "Chinese Yuan",
    "5. Exchange Rate": "252335.42242500",
    "6. Last Refreshed": "2021-06-14 06:05:06",
    "7. Time Zone": "UTC",
    "8. Bid Price": "252335.35845800",
    "9. Ask Price": "252335.42242500"
}

}

我想从键值对中删除所有编号,所以它最终应该看起来像这样

{
"Realtime Currency Exchange Rate": {
    "From_Currency Code": "BTC",
    "From_Currency Name": "Bitcoin",
    "To_Currency Code": "CNY",
    "To_Currency Name": "Chinese Yuan",
    "Exchange Rate": "252335.42242500",
    "Last Refreshed": "2021-06-14 06:05:06",
    "Time Zone": "UTC",
    "Bid Price": "252335.35845800",
    "Ask Price": "252335.42242500"
}

}

我真的很感谢任何帮助实现这个目标?谢谢。

方法很简单。我们获取 json 对象的每个键,并将其数字部分替换为空字符串,然后将旧键的数据复制到新键并删除旧键。

const json=`{
  "Realtime Currency Exchange Rate": {
    "1. From_Currency Code": "BTC",
    "2. From_Currency Name": "Bitcoin",
    "3. To_Currency Code": "CNY",
    "4. To_Currency Name": "Chinese Yuan",
    "5. Exchange Rate": "252335.42242500",
    "6. Last Refreshed": "2021-06-14 06:05:06",
    "7. Time Zone": "UTC",
    "8. Bid Price": "252335.35845800",
    "9. Ask Price": "252335.42242500"
  }
}`;

var obj = JSON.parse(json);
const index = 'Realtime Currency Exchange Rate';

Object.keys(obj[index]).forEach(key => {
   var newKey = key.replace(/\d+\. /g, '');
   obj[index][newKey] = obj[index][key];
   delete obj[index][key];
});

console.log(obj);