TypeError:mappingproxy 类型的对象不是 JSON 可序列化的
Posted
技术标签:
【中文标题】TypeError:mappingproxy 类型的对象不是 JSON 可序列化的【英文标题】:TypeError: Object of type mappingproxy is not JSON serializable 【发布时间】:2021-12-10 05:29:46 【问题描述】:我在尝试将类对象转换为 JSON 格式时遇到问题。 实际上,我有一个 ECG 类对象,我希望将该对象转换为 JSON 格式的字符串。
例如:“Source”:“MIT”,“FileName”:“100”,“Channel”:2,“Record”:11520000,“Time”:1800,“SampleRate”:500
ECGModel.py
class ECG(object):
@classmethod
def __init__(self, source, fileName, channel, record, time, sampleRate, ecg):
self.Source = source
self.FileName = fileName
self.Channel = channel
self.Record = record
self.Time = time
self.SampleRate = sampleRate
self.ECG = ecg
# getting the values
@property
def value(self):
print('Getting value')
return self.Source, self.FileName, self.Channel, self.Record, self.Time, self.SampleRate, self.ECG
# setting the values
@value.setter
def value(self, source, fileName, channel, record, time, sampleRate, ecg):
print('Setting value to ' + source)
self.Source = source
self.FileName = fileName
self.Channel = channel
self.Record = record
self.Time = time
self.SampleRate = sampleRate
self.ECG = ecg
# deleting the values
@value.deleter
def value(self):
print('Deleting value')
del self.Source, self.Source, self.FileName, self.Channel, self.Record, self.Time, self.SampleRate, self.ECG
main.py
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)
jsonECGPropertyStr = json.dumps(ecgProperty.__dict__)
st.write(jsonECGPropertyStr)
处理器.py
import streamlit as st
import Controllers.ECGModel as ecgModel
def GetSourceProperty(filePath):
ecg = ecgModel.ECG
ecg.Source = "MIT"
ecg.FileName = "100"
ecg.Channel = 2
ecg.Record = 11520000
ecg.Time = 1800
ecg.SampleRate = 500
return ecg
日志:
<class 'type'>
<class 'Controllers.ECGModel.ECG'>
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\site-packages\streamlit\script_runner.py", line 354, in _run_script
exec(code, module.__dict__)
File "D:\SourceCode\BIS2019_MasterThesis\ECG_Evaluation\Main.py", line 27, in <module>
jsonECGPropertyStr = json.dumps(ecgProperty.__dict__)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\xxx\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type o.__class__.__name__ '
TypeError: Object of type mappingproxy is not JSON serializable
【问题讨论】:
json.dumps(ecgProperty.__dict__)
的预期输出是什么?包含 source, FileName, ...
值的 json 字符串?
@ShihabusSakibRad 是的,没错
【参考方案1】:
我试图猜测您要达到的目标。看看下面的代码是否适合你
ECGModel.py
class ECG:
def __init__(self, source, file_name, channel, record, time, sample_rate):
self.source = source
self.file_name = file_name
self.channel = channel
self.record = record
self.time = time
self.sample_rate = sample_rate
处理器.py
from ECGModel import ECG
def GetSourceProperty():
return ECG(source="MIT", file_name="100", channel=2, record=11520000, time=1800, sample_rate=500)
主要.py:
您可以将print
替换为您的st.write
import Processor
import json
ecg = Processor.GetSourceProperty()
print(type(ecg))
print(ecg)
print(json.dumps(ecg.__dict__))
输出
<class 'ECGModel.ECG'>
<ECGModel.ECG object at 0x000002ED4E82B670>
"source": "MIT", "file_name": "100", "channel": 2, "record": 11520000, "time": 1800, "sample_rate": 500
【讨论】:
谢谢。我在我的代码中发现了 2 个问题。 1.@classmethod
引起了问题(如果我把它放在那里,输出将为空)。 2. 所有属性都必须像它们的参数一样——self.source = source
——应该是“source”而不是“Source”
@HuyDQ 不,属性名称与参数名称相同不是强制性的。你可以做self.source_name = source
,在这种情况下,Json 将包含source_name
。如果你是 python 新手,你可以阅读关于classmethod、init、attribute name以上是关于TypeError:mappingproxy 类型的对象不是 JSON 可序列化的的主要内容,如果未能解决你的问题,请参考以下文章
TypeError: 'float' 类型的对象没有 len() & TypeError: 'float' 对象不可迭代
TypeError:'Cursor' 类型的对象没有 len()