Skip to main content
 首页 » 编程设计

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

2024年12月31日9EasonJim

这是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.