javascript - 使用 JSON 在 Python 和 JavaScript 之间进行通信以更改网站的 CSS
问题描述
我在解析与 javascript 相同路径中的 JSON 文件时遇到问题。我已经用下面的代码试过了
var request = new XMLHttpRequest();
request.open('GET', 'data.json')
request.responseType = 'json';
request.send();
request.onload = function() {
var data = request.response;
var jsondata = JSON.parse(data);
return jsondata;
}
console.log(jsondata['text_06']);
但我只得到错误: Uncaught ReferenceError: jsondata is not defined at webvisu.js:101
在这种情况下,“text_06”是来自 JSON 文件的字符串,如下所示:
{
"part_count_07": 0,
"packaging_05": false,
"emergency_stop_06": false,
"start_stop_08": false,
"error_07": false,
"beg_06": false,
"beg2_05": false,
"warning_08": false,
"text_06": "magazine nearly full",
"emergency_stop_08": false,
"warning_07": false,
"emergency_stop_05": true,
"information_05": false,
"text_05": "emergency-stop",
"mid1_05": false,
"start_stop_07": false,
"packing_07": false,
"part_count_05": 9,
"start_stop_06": false,
"end1_05": true,
"emergency_stop_07": false,
"error_06": false,
"packing_08": false,
"beg1_05": false,
"beg_07": false,
"information_08": false,
"error_05": false,
"warning_05": false,
"information_06": false,
"part_count_06": 10,
"mid_08": true,
"warning_06": false,
"end_07": false,
"mid_07": false,
"end2_05": true,
"beg_08": true,
"text_07": "",
"information_07": false,
"extract_count": 0,
"error_08": false,
"end_08": false,
"part_count_08": 1,
"text_08": " ",
"start_stop_05": false,
"color_07": 0
}
我的目标是使用 JSON 文件中的数据更改网站上的文本和颜色,该文件每秒被以下 Python 脚本覆盖,该脚本已经完全正常运行:
while True:
with open('data.json') as f:
json_data = json.load(f)
if extract_count == 0:
ext_note = True
# Storing Station Data ST07
db_07 = plc_07.db_read(DB_NUM_07, START_ADDRESS, SIZE_07)
error_07 = (get_bool(db_07, 0, 0))
warning_07 = (get_bool(db_07, 0, 1))
information_07 = (get_bool(db_07, 0, 2))
part_count_07 = (int.from_bytes(db_07[2:4], byteorder='big'))
start_stop_07 = (get_bool(db_07, 4, 0))
emergency_stop_07 = (get_bool(db_07, 4, 1))
beg_07 = (get_bool(db_07, 4, 2))
mid_07 = (get_bool(db_07, 4, 3))
end_07 = (get_bool(db_07, 4, 4))
packing_07 = (get_bool(db_07, 4, 5))
color_07 = (int.from_bytes(db_07[6:8], byteorder='big'))
extract_count = (int.from_bytes(db_07[8:10], byteorder='big'))
text_07 = (get_string(db_07, 10, 266))
if ext_note:
set_int(db_07, 8, (json_data['extract_count']))
set_int(db_07, 6, (json_data['color_07']))
ext_note = False
set_bool(db_07, 4, 0, (json_data['start_stop_07']))
set_bool(db_07, 4, 5, (json_data['packing_07']))
set_bool(db_07, 4, 6, True)
plc_07.db_write(DB_NUM_07, START_ADDRESS, db_07)
# Seperating Station Data ST05
db_05 = plc_05.db_read(DB_NUM_05, START_ADDRESS, SIZE_05)
error_05 = (get_bool(db_05, 4, 2))
warning_05 = (get_bool(db_05, 4, 1))
information_05 = (get_bool(db_05, 4, 0))
packaging_05 = (get_bool(db_05, 0, 1))
part_count_05 = (int.from_bytes(db_05[2:4], byteorder='big'))
start_stop_05 = (get_bool(db_05, 0, 0))
emergency_stop_05 = (get_bool(db_05, 5, 0))
beg1_05 = (get_bool(db_05, 4, 3))
mid1_05 = (get_bool(db_05, 4, 4))
end1_05 = (get_bool(db_05, 4, 5))
beg2_05 = (get_bool(db_05, 4, 6))
end2_05 = (get_bool(db_05, 4, 7))
text_05 = (get_string(db_05, 6, 262))
set_bool(db_07, 5, 1, True)
# Packaging Station Data ST08
db_08 = plc_08.db_read(DB_NUM_08, START_ADDRESS, SIZE_08)
error_08 = (get_bool(db_08, 0, 0))
warning_08 = (get_bool(db_08, 0, 1))
information_08 = (get_bool(db_08, 0, 2))
part_count_08 = (int.from_bytes(db_08[2:4], byteorder='big'))
start_stop_08 = (get_bool(db_08, 4, 0))
emergency_stop_08 = (get_bool(db_08, 4, 1))
beg_08 = (get_bool(db_08, 4, 2))
mid_08 = (get_bool(db_08, 4, 3))
end_08 = (get_bool(db_08, 4, 4))
packing_08 = (get_bool(db_08, 4, 5))
text_08 = (get_string(db_08, 6, 262))
set_bool(db_07, 4, 6, True)
# Handling Station Data ST06
db_06 = plc_06.db_read(DB_NUM_06, START_ADDRESS, SIZE_06)
error_06 = (get_bool(db_06, 0, 0))
warning_06 = (get_bool(db_06, 0, 1))
information_06 = (get_bool(db_06, 0, 2))
part_count_06 = (int.from_bytes(db_06[2:4], byteorder='big'))
start_stop_06 = (get_bool(db_06, 4, 0))
emergency_stop_06 = (get_bool(db_06, 4, 1))
beg_06 = (get_bool(db_06, 4, 2))
text_06 = (get_string(db_06, 6, 262))
set_bool(db_07, 4, 3, True)
json_data = {
"error_07": error_07,
"warning_07": warning_07,
"information_07": information_07,
"part_count_07": part_count_07,
"start_stop_07": start_stop_07,
"emergency_stop_07": emergency_stop_07,
"beg_07": beg_07,
"mid_07": mid_07,
"end_07": end_07,
"text_07": text_07,
"packing_07": packing_07,
"color_07": color,
"extract_count": extract_count,
"error_05": error_05,
"warning_05": warning_05,
"information_05": information_05,
"packaging_05": packaging_05,
"part_count_05": part_count_05,
"start_stop_05": start_stop_05,
"emergency_stop_05": emergency_stop_05,
"beg1_05": beg1_05,
"mid1_05": mid1_05,
"end1_05": end1_05,
"beg2_05": beg2_05,
"end2_05": end2_05,
"text_05": text_05,
"error_08": error_08,
"warning_08": warning_08,
"information_08": information_08,
"part_count_08": part_count_08,
"start_stop_08": start_stop_08,
"emergency_stop_08": emergency_stop_08,
"beg_08": beg_08,
"mid_08": mid_08,
"end_08": end_08,
"packing_08": packing_08,
"text_08": text_08,
"error_06": error_06,
"warning_06": warning_06,
"information_06": information_06,
"part_count_06": part_count_06,
"start_stop_06": start_stop_06,
"emergency_stop_06": emergency_stop_06,
"beg_06": beg_06,
"text_06": text_06
}
json.dump(json_data, open('data.json', 'w'), indent=1)
print(json.dumps(json_data, indent=1))
t.sleep(1)
所以我的问题是如何正确解析 JavaScript 中的 JSON 文件并在脚本中使用它的数据。甚至可以永久读取 JSON 文件并根据新数据更新网站吗?
解决方案
您可能会在加载数据时调用一个函数
request.onload = function() {
var data = request.response;
var jsondata = JSON.parse(data);
dataLoaded(jsondata);
}
function dataLoaded(jsondata){
console.log(jsondata['text_06']);
}
推荐阅读
- ios - 原生到 React Native 调用
- spring-boot - 在 EC2 上启用 SSL 是否必须使用 Amazon Route 54?
- python - 从熊猫的字符串日期时间列中提取日期
- flutter - 在颤动中调整容器大小以精确到屏幕大小的一半
- javascript - 重置用于打印的 javascript 结果
- javascript - Visjs/React:动态构建数据集
- java - 错误:java:错误:无效的源版本:13 使用 JDK12 和 IntelliJ
- php - 我的代码出现数据库有什么问题?
- swift - MKPointAnnotation 到 MKMapItem
- python - 无法在pygame中在全屏和窗口模式之间切换