json.dump()不接受dict的列表

json.dump()不接受dict的列表,json,python-3.x,Json,Python 3.x,我在编写json文件时遇到问题 我有一个目录,目录里有另一个目录(有一个目录)。代码是 import json results.append("text": [line_1, line_2,...], "date": "today" "meta": {"field_01": line_3,

我在编写json文件时遇到问题 我有一个目录,目录里有另一个目录(有一个目录)。代码是

import json
results.append("text": [line_1, line_2,...],
               "date": "today"
               "meta": {"field_01": line_3,
                        "field_02": [item.keys() for item in dict_list_of_dict],
                       }})

with open("file_name", "w", encoding="utf-8") as f:
    json.dump(results, f)
错误消息:

TypeError:类型为“dict_items”的对象不可JSON序列化

有什么建议可以解决这个问题吗

谢谢

安德烈亚斯

===== 回溯:

> TypeError                                 Traceback (most recent call last)
<ipython-input-182-acb18c0e0c91> in <module>
      1 with open(os.path.join("policy-text-MiddleEast-all2010-2020-entityPERSON" + ".json"), 
      2           "w") as f:
----> 3     json.dump(result_new[0], f)

/usr/lib/python3.6/json/__init__.py in dump(obj, fp, skipkeys, ensure_ascii, check_circular, allow_nan, cls, indent, separators, default, sort_keys, **kw)
    177     # could accelerate with writelines in some versions of Python, at
    178     # a debuggability cost
--> 179     for chunk in iterable:
    180         fp.write(chunk)
    181 

/usr/lib/python3.6/json/encoder.py in _iterencode(o, _current_indent_level)
    428             yield from _iterencode_list(o, _current_indent_level)
    429         elif isinstance(o, dict):
--> 430             yield from _iterencode_dict(o, _current_indent_level)
    431         else:
    432             if markers is not None:

/usr/lib/python3.6/json/encoder.py in _iterencode_dict(dct, _current_indent_level)
    402                 else:
    403                     chunks = _iterencode(value, _current_indent_level)
--> 404                 yield from chunks
    405         if newline_indent is not None:
    406             _current_indent_level -= 1

/usr/lib/python3.6/json/encoder.py in _iterencode_dict(dct, _current_indent_level)
    402                 else:
    403                     chunks = _iterencode(value, _current_indent_level)
--> 404                 yield from chunks
    405         if newline_indent is not None:
    406             _current_indent_level -= 1

/usr/lib/python3.6/json/encoder.py in _iterencode(o, _current_indent_level)
    435                     raise ValueError("Circular reference detected")
    436                 markers[markerid] = o
--> 437             o = _default(o)
    438             yield from _iterencode(o, _current_indent_level)
    439             if markers is not None:

/usr/lib/python3.6/json/encoder.py in default(self, o)
    178         """
    179         raise TypeError("Object of type '%s' is not JSON serializable" %
--> 180                         o.__class__.__name__)
    181 
    182     def encode(self, o):
>类型错误回溯(最近一次调用)
在里面
1带有open(os.path.join(“policy-text-middleast-all2010-2020-entityPERSON”+“.json”),
2“w”)作为f:
---->3.json.dump(result_new[0],f)
/转储文件中的usr/lib/python3.6/json/_init___;.py(obj、fp、skipkeys、sure_ascii、check_circular、allow_nan、cls、indent、separators、default、sort_key、**kw)
177#可以在某些版本的Python中使用writeline进行加速,至少
178#可调试性成本
-->179对于iterable中的块:
180 fp.写入(块)
181
/usr/lib/python3.6/json/encoder.py在iterencode中(o,当前缩进级别)
428来自_iterencode_列表的产量(o,_当前_缩进水平)
429 elif isinstance(o,dict):
-->430从指令(o,当前缩进水平)中获得的收益
431其他:
432如果标记不是无:
/usr/lib/python3.6/json/encoder.py在iterencode目录中(dct,当前缩进级)
402其他:
403块=\u iterencode(值,\u当前\u缩进\u级别)
-->404块的产量
405如果换行符缩进不是无:
406当前缩进级别-=1
/usr/lib/python3.6/json/encoder.py在iterencode目录中(dct,当前缩进级)
402其他:
403块=\u iterencode(值,\u当前\u缩进\u级别)
-->404块的产量
405如果换行符缩进不是无:
406当前缩进级别-=1
/usr/lib/python3.6/json/encoder.py在iterencode中(o,当前缩进级别)
435提升值错误(“检测到循环参考”)
436标记[markerid]=o
-->437 o=_默认值(o)
438从_iterencode(o,_当前_缩进_级别)得到的收益率
439如果标记不是无:
/默认情况下的usr/lib/python3.6/json/encoder.py(self,o)
178         """
179 raise TypeError(“类型“%s”的对象不可JSON序列化”%
-->180度。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
181
182 def编码(自身,o):

item.keys()
返回a,而
json
模块不知道如何处理它。将其转换为列表:
list(item.keys())

item.keys()
返回a,而
json
模块不知道如何处理它。将其转换为列表:
list(item.keys())

这可能有助于确定您的实际代码?错误消息暗示数据包含
dict.items
item.keys()的结果
将产生类似于
类型错误的消息:dict_keys类型的对象不是JSON可序列化的
。谢谢,它与您的修改一起工作。我只是想我已经编写了这么好的压缩代码;-)这可能有助于您的实际代码?错误消息暗示数据包含
dict.items
的结果。
item.keys()
将产生类似于
TypeError:dict\u keys类型的对象不可JSON序列化的消息。
。谢谢,它与您的修改一起工作。我刚才还以为我写了这么好的压缩代码;-)