我在Python中创建了一个JSON对象,并将其发送到我的JavaScript文件中,以便在网站上显示该值。
我已经试过用单引号,双引号之间的单引号
InfosThymio = {"proxh": "N/A"}然后我发过来:
def getInfos():
global infosThymio
return str(infosThymio)我的问题是,JavaScript会这样做:
{'proxh':'N/A'}和我得到了unexpected token ' at position 1
在JavaScript中,我使用这种方式获取JSON并解析它:
getRobotData() {
api.get()
.then((data) => data.json())
.then((res) => {
const data = JSON.parse(res);
this.setCapteur('hProxi', data.proxh);发布于 2019-01-23 14:22:12
您不需要编写const data = JSON.parse(res);,因为.then((data) => data.json())行已经解析了JSON
.then((data) => data.json())行告诉计算机解析来自API的JSON响应并返回结果的javascript对象。
将您的变量命名为这样的名称在语义上更正确:
getRobotData() {
api.get()
.then((res) => res.json()) // The JSON response is parsed into data
.then((data) => {
// Do something with your datahttps://stackoverflow.com/questions/54329301
复制相似问题