【发布时间】:2021-08-24 07:03:35
【问题描述】:
我正在访问如下所示的 JSON。
[
{
"itemType": "SelectionTitle",
"_id": "5ada2217c114ca048e1db9b0",
"created_by": "5ab57289d8d00507b29a3fdd",
"selectionFile": {
"item.Type": "SelectionFile",
"name": "1105F.MID",
"active": true,
"isFactory.Default": false,
"selection.Type": "Music",
"sfzFile": "",
"destination": "/data/uploads",
"encoding": "7bit",
"fieldname": "file",
"filename": "782f49a7cd72b865b4e2d286816792e7"
...
}
}, ...
我在将名称中包含. 的对象键重命名为_ 时遇到问题。例如:
item.Type 或 selection.Type 到 item_Type 或 selection_Type。
这是我想要使用的:
var json = jsonFromExampleAbove;
str = JSON.stringify(json);
str = str.selectionFile.replace(/\".\":/g, "\"_\":");
json = JSON.parse(str);
console.log(json);
我收到控制台日志错误。我认为这是因为我试图替换的值是嵌套的,但不确定。我在这里仍然是一个初学者。
谢谢。
【问题讨论】:
-
str.selectionFile.您将对象转换为 JSON 文本字符串,字符串上没有selectionFile属性。此外,您的正则表达式是错误的,它正在搜索您的任何键都没有的".":。
标签: javascript arrays json object