gpt4 book ai didi

python - json 文件中的 "Expecting property name enclosed in double quotes"

转载 作者:太空宇宙 更新时间:2023-11-04 04:26:26 28 4
gpt4 key购买 nike

这是json文件

{"pre_trigger": 4, "sampling frequency": 1652, "record length": 15.0, 
"sensors":
[{"model": "393B05", "serial": "46978", "sensitivity": 10030, "sensitivity_units": "mV/g", "sensor_type": "Accelerometer", "units": "g", "location": [7.01, -0.19, 0], "location_units": "m", "direction": [0, 0, 1], "trigger": true, "trigger_value": 0.005, "max_val": 0.45, "min_val": -0.45, "comments": "Inside B122 next to bookshelf", "channel": "cDAQ1Mod2/ai0"}],

[{"model": "393B05", "serial": "47085", "sensitivity": 9980, "sensitivity_units": "mV/g", "sensor_type": "Accelerometer", "units": "g", "location": [9.65, -0.19, 0], "location_units": "m", "direction": [0, 0, 1], "trigger": true, "trigger_value": 0.005, "max_val": 0.45, "min_val": -0.45, "comments": "Inside B122 under the whiteboard", "channel": "cDAQ1Mod2/ai1"}]

"parameters": {"general": [], "specific": ["Walking direction", "Person ID"]}}

我不是一个懂编码的人,所以我不知道这个错误的真正来源。我正在运行以下命令

daq = DAQ()
daq.load_setup('json.fname')

返回属性错误。 json文件里没有单引号,真不知道问题出在哪里。下面是错误回调的位置。

 def load_setup(self,fname='setup.json'):
"""
Opens the JSON file containing the setup parameters for the experiment.

Parameters
----------
fname : str
File that the parameters for the experiment were saved into (JSON file)

"""
import json

with open(fname, 'r') as setup_file:
setup_data = json.load(setup_file)

self.fs = setup_data['sampling frequency']
self.record_length = setup_data['record length']
self.sensors = setup_data['sensors']
self.parameters = setup_data['parameters']
self.pre_trigger = setup_data['pre_trigger']

最佳答案

您根本没有有效的 JSON(您的 Python 代码没有任何问题)。您没有正确使用数组功能。 JSON 数组如下所示:

{"some_array": ["first item", "second item", ..., "last item"]}

看起来像这样(这就是你所拥有的以及为什么你得到你得到的错误):

{"some_array": ["first item"], ["second item"], ..., ["last item"]}

长话短说,您的列表项方括号内用逗号分隔。这是您的 JSON 应该看起来的样子(sensor 数组固定且打印漂亮):

{
"pre_trigger": 4,
"sampling frequency": 1652,
"record length": 15.0,
"sensors":
[
{
"model": "393B05",
"serial": "46978",
"sensitivity": 10030,
"sensitivity_units": "mV/g",
"sensor_type": "Accelerometer",
"units": "g",
"location": [7.01, -0.19, 0],
"location_units": "m",
"direction": [0, 0, 1],
"trigger": true,
"trigger_value": 0.005,
"max_val": 0.45,
"min_val": -0.45,
"comments": "Inside B122 next to bookshelf",
"channel": "cDAQ1Mod2/ai0"
},
{
"model": "393B05",
"serial": "47085",
"sensitivity": 9980,
"sensitivity_units": "mV/g",
"sensor_type": "Accelerometer",
"units": "g",
"location": [9.65, -0.19, 0],
"location_units": "m",
"direction": [0, 0, 1],
"trigger": true,
"trigger_value": 0.005,
"max_val": 0.45,
"min_val": -0.45,
"comments": "Inside B122 under the whiteboard",
"channel": "cDAQ1Mod2/ai1"
}
],

"parameters": {
"general": [],
"specific":
[
"Walking direction",
"Person ID"
]
}
}

我建议始终保持您的 JSON 打印精美(即使在磁盘上),因为它更易于阅读/理解。 JSON 格式的部分吸引力在于,您可以像人类一样轻松地观察它。

您发布的其余代码在此修复后工作正常。

HTH.

关于python - json 文件中的 "Expecting property name enclosed in double quotes",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53416320/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com