javascript - 从 .js 文件中获取数据
问题描述
我有一个Json_data.txt
存储在本地的文件。该文件的内容是一个 JSON 对象,如下所示:
{
"directed": "true",
"multigraph": "true",
"graph": {"directed": "true"},
"nodes": [
{"id": "Brave Browser", "shape": "circle"},
{"id": "Windows Explorer", "shape": "circle"},
{"id": "Spotify", "shape": "circle"},
{"id": "Windows Shell Experience Host", "shape": "circle"},
{"id": "Timeular", "shape": "circle"}
],
"links": [
{"source": "Brave Browser", "target": "Brave Browser", "time": 4, "weight": 4},
{"source": "Brave Browser", "target": "Windows Explorer", "time": 2, "weight": 2},
{"source": "Windows Explorer", "target": "Spotify", "time": 1, "weight": 1},
{"source": "Spotify", "target": "Spotify", "time": 1, "weight": 1},
{"source": "Spotify", "target": "Windows Explorer", "time": 1, "weight": 1},
{"source": "Windows Explorer", "target": "Windows Shell Experience Host", "time": 1, "weight": 1},
{"source": "Windows Shell Experience Host", "target": "Brave Browser", "time": 1, "weight": 1},
{"source": "Windows Explorer", "target": "Timeular", "time": 1, "weight": 1},
{"source": "Timeular", "target": "Timeular", "time": 1, "weight": 1},
{"source": "Timeular", "target": "Brave Browser", "time": 1, "weight": 1}
]
}
我正在尝试在 HTML 页面中读取上述 JSON 以进行进一步处理。我尝试src
在脚本标签中使用来链接文件并尝试将其解析为"script type="text/javascript" src="file:///C:/file_path/Json_data.txt"
. 我正在尝试将内容放入test
:
fetch('file:///C:/file_path/Json_data.txt')
.then(response => response.json())
.then(test => JSON.parse(json));
上面的代码不起作用。谁能告诉我我在这里做错了什么?