"Expecting property name enclosed in double quotes" 在 json 文件中
"Expecting property name enclosed in double quotes" in json file
这是 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')
其中returns属性错误。 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 印刷精美(即使在磁盘上),因为这样更容易 read/understand。 JSON 格式的部分吸引力在于您可以像人类一样轻松地观察它。
您发布的其余代码在此修复后工作正常。
HTH.
这是 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')
其中returns属性错误。 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 印刷精美(即使在磁盘上),因为这样更容易 read/understand。 JSON 格式的部分吸引力在于您可以像人类一样轻松地观察它。
您发布的其余代码在此修复后工作正常。
HTH.