You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ECG类对象转JSON时报mappingproxy类型无法JSON序列化问题求助

问题根因
  1. 你没有实例化ECG类,而是直接把类本身赋值给了ecg变量:ecg = ecgModel.ECG 取到的是类对象,不是类的实例对象,类的__dict__属性是只读的mappingproxy类型,默认不支持JSON序列化。
  2. ECG类的__init__方法被错误添加了@classmethod装饰器,构造方法是实例方法,不需要加类装饰器,加了之后就算你尝试实例化类也会报错。
  3. 你定义的value属性的setter语法错误,setter只能接收1个额外的赋值参数,你定义了多个参数,后续调用会报错。
修复步骤

1. 修复ECGModel.py代码

class ECG(object):
    # 移除错误的@classmethod装饰器,给ecg参数加默认值兼容不传的场景
    def __init__(self, source, fileName, channel, record, time, sampleRate, ecg=None):
        self.Source = source
        self.FileName = fileName
        self.Channel = channel
        self.Record = record
        self.Time = time
        self.SampleRate = sampleRate
        self.ECG = ecg

    @property
    def value(self):
        print('Getting value')
        return self.Source, self.FileName, self.Channel, self.Record, self.Time, self.SampleRate, self.ECG

    # 不需要setter可以直接注释/删除,要用的话调整为接收单参数后解包
    # @value.setter
    # def value(self, args):
    #     source, fileName, channel, record, time, sampleRate, ecg = args
    #     self.Source = source
    #     self.FileName = fileName
    #     self.Channel = channel
    #     self.Record = record
    #     self.Time = time
    #     self.SampleRate = sampleRate
    #     self.ECG = ecg

    @value.deleter
    def value(self):
        print('Deleting value')
        del self.Source, self.FileName, self.Channel, self.Record, self.Time, self.SampleRate, self.ECG

2. 修复Processor.py的实例化逻辑

import streamlit as st
import Controllers.ECGModel as ecgModel

def GetSourceProperty(filePath):
    # 实例化ECG类,不要直接赋值类本身
    ecg = ecgModel.ECG(
        source = "MIT",
        fileName = "100",
        channel = 2,
        record = 11520000,
        time = 1800,
        sampleRate = 500
    )
    return ecg

3. 调整Main.py的序列化逻辑

手动构造目标字典,刚好匹配你需要的输出格式,避免带入不需要的ECG字段:

import streamlit as st
import Processor as processor
import json

ecgProperty = processor.GetSourceProperty(r"C:\Users\100.dat")

st.write(type(ecgProperty))
st.write(ecgProperty)
# 手动构造需要序列化的字段,格式完全匹配预期
serialized_dict = {
    "Source": ecgProperty.Source,
    "FileName": ecgProperty.FileName,
    "Channel": ecgProperty.Channel,
    "Record": ecgProperty.Record,
    "Time": ecgProperty.Time,
    "SampleRate": ecgProperty.SampleRate
}
jsonECGPropertyStr = json.dumps(serialized_dict)
st.write(jsonECGPropertyStr)

内容的提问来源于stack exchange,提问作者Huy DQ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 14:54:01