json fi中的“期望属性名括在双引号中”

2024-05-08 00:18:51 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: theselfjsondatasetuplocationvalrecord
1条回答
网友
1楼 · 发布于 2024-05-08 00:18:51

您只是没有有效的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格式的一部分吸引力在于,您可以轻松地将其视为一个人。你知道吗

在此修复之后,您发布的其余代码运行良好。你知道吗

嗯。你知道吗

相关问题 更多 >