我应该读取一个Python数据结构,一个精确到JavaScript函数的2d列表。我知道这可以在将python数据结构转换为json对象然后使用Javascript函数读取json对象之后完成。我正在使用Python脚本呈现html页面,如下所示。使用Python和Javascript从Json文件读取和写入
import webbrowser, json
f = open('World.html', 'w')
arr = [[]]
arr[0].append('Hello')
arr[0].append('There')
arr.append([])
arr[1].append('Good')
arr[1].append('Morning')
arr[1].append('!')
message = '''<html><head><script type="text/javascript" src="outputjson.json"></script><script>function Hello(){alert(outputjson.arr);}</script></head><body><h1>This is not working and you know it</h1><input value="Click Bro" type="button" onclick="Hello();"></input></body></html>'''
f.write(message)
with open('outputjson.json', 'w') as f:
json.dump(arr, f)
f.close()
webbrowser.open_new_tab('World.html')
Outputjson.json是这个样子: [ “!”[ “你好”, “有”],[ “好”, “早上好”,]]
JSON对象会成功创建,但我无法通过我的JavaScript函数读取它。我哪里错了? 我有一个约束,我不能去BeautifulSoup。 在此先感谢。
任何错误信息? –
不,我不能看到任何警报消息。如果我明显硬编码,警报消息将起作用。 –
你不能像这样加载json。 json将被评估为js,并且它将不可用 – karthick