json文件中的“期望属性名称用双引号引起来”

时间:2018-11-21 16:17:38

标签: python json quotes

这是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']

1 个答案:

答案 0 :(得分:1)

您只是没有有效的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。