所以我使用的是 Python 2.7,使用 json
模块对以下数据结构进行编码:
'layer1': {
'layer2': {
'layer3_1': [ long_list_of_stuff ],
'layer3_2': 'string'
}
}
我的问题是我正在使用 pretty-print 来打印所有内容,如下所示:
json.dumps(data_structure, indent=2)
这很好,除了我想缩进所有内容,除了 "layer3_1"
中的内容 — 这是一个列出坐标的海量字典,因此,每个坐标都设置一个值让 pretty-print 创建一个包含数千行的文件,示例如下:
{
"layer1": {
"layer2": {
"layer3_1": [
{
"x": 1,
"y": 7
},
{
"x": 0,
"y": 4
},
{
"x": 5,
"y": 3
},
{
"x": 6,
"y": 9
}
],
"layer3_2": "string"
}
}
}
我真正想要的是类似于以下内容:
{
"layer1": {
"layer2": {
"layer3_1": [{"x":1,"y":7},{"x":0,"y":4},{"x":5,"y":3},{"x":6,"y":9}],
"layer3_2": "string"
}
}
}
我听说可以扩展 json
模块:是否可以将其设置为仅在 "layer3_1"
对象内时关闭缩进?如果是这样,有人能告诉我怎么做吗?
最佳答案
(注意:
此答案中的代码仅适用于返回 JSON 格式字符串的 json.dumps()
,但 not 与 json.dump()
一起使用直接写入类文件对象。在我对问题 Write two-dimensional list to JSON file 的回答中,有一个修改版本适用于两者。 .)
更新
以下是我的原始答案的一个版本,经过多次修改。与原版不同,我发布它只是为了展示如何在 J.F.Sebastian 的 answer 中获得第一个想法工作,并且像他一样,返回对象的非缩进 string 表示。最新更新的版本返回单独格式化的 Python 对象 JSON。
每个坐标 dict
的键将按照 OP 的注释按排序顺序出现,但前提是在 sort_keys=True
关键字参数中指定初始 json.dumps()
调用驱动该过程,并且它不再沿途将对象的类型更改为字符串。换句话说,“包装”对象的实际类型现在得到了维护。
我认为不理解我的帖子的初衷导致很多人对其投反对票 - 因此,主要出于这个原因,我已经“修复”并改进了我的答案几次。当前版本是我的原始答案与@Erik Allik 在他的answer 中使用的一些想法的混合体。 ,以及此答案下方的评论中显示的其他用户的有用反馈。
以下代码在 Python 2.7.16 和 3.7.4 中似乎都没有改变。
from _ctypes import PyObj_FromPtr
import json
import re
class NoIndent(object):
""" Value wrapper. """
def __init__(self, value):
self.value = value
class MyEncoder(json.JSONEncoder):
FORMAT_SPEC = '@@{}@@'
regex = re.compile(FORMAT_SPEC.format(r'(\d+)'))
def __init__(self, **kwargs):
# Save copy of any keyword argument values needed for use here.
self.__sort_keys = kwargs.get('sort_keys', None)
super(MyEncoder, self).__init__(**kwargs)
def default(self, obj):
return (self.FORMAT_SPEC.format(id(obj)) if isinstance(obj, NoIndent)
else super(MyEncoder, self).default(obj))
def encode(self, obj):
format_spec = self.FORMAT_SPEC # Local var to expedite access.
json_repr = super(MyEncoder, self).encode(obj) # Default JSON.
# Replace any marked-up object ids in the JSON repr with the
# value returned from the json.dumps() of the corresponding
# wrapped Python object.
for match in self.regex.finditer(json_repr):
# see https://stackoverflow.com/a/15012814/355230
id = int(match.group(1))
no_indent = PyObj_FromPtr(id)
json_obj_repr = json.dumps(no_indent.value, sort_keys=self.__sort_keys)
# Replace the matched id string with json formatted representation
# of the corresponding Python object.
json_repr = json_repr.replace(
'"{}"'.format(format_spec.format(id)), json_obj_repr)
return json_repr
if __name__ == '__main__':
from string import ascii_lowercase as letters
data_structure = {
'layer1': {
'layer2': {
'layer3_1': NoIndent([{"x":1,"y":7}, {"x":0,"y":4}, {"x":5,"y":3},
{"x":6,"y":9},
{k: v for v, k in enumerate(letters)}]),
'layer3_2': 'string',
'layer3_3': NoIndent([{"x":2,"y":8,"z":3}, {"x":1,"y":5,"z":4},
{"x":6,"y":9,"z":8}]),
'layer3_4': NoIndent(list(range(20))),
}
}
}
print(json.dumps(data_structure, cls=MyEncoder, sort_keys=True, indent=2))
输出:
{
"layer1": {
"layer2": {
"layer3_1": [{"x": 1, "y": 7}, {"x": 0, "y": 4}, {"x": 5, "y": 3}, {"x": 6, "y": 9}, {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7, "i": 8, "j": 9, "k": 10, "l": 11, "m": 12, "n": 13, "o": 14, "p": 15, "q": 16, "r": 17, "s": 18, "t": 19, "u": 20, "v": 21, "w": 22, "x": 23, "y": 24, "z": 25}],
"layer3_2": "string",
"layer3_3": [{"x": 2, "y": 8, "z": 3}, {"x": 1, "y": 5, "z": 4}, {"x": 6, "y": 9, "z": 8}],
"layer3_4": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
}
}
}
https://stackoverflow.com/questions/13249415/
相关文章:
ios - 如何在 Swift 中解析 JSON 中的数组?
ios - (Cocoa 错误 3840。)“(字符 0 周围的值无效。)AFNetworking
c# - 如何使用 Newtonsoft.Json 包在 C#(4.0) 中解析我的 json 字符
python - 使用 simplejson 序列化简单类对象的最简单方法?
javascript - 如何使用 Javascript 将数据写入 JSON 文件
json - Golang 将 JSON 数组解析成数据结构
java - 如何在 Java 的 JSON 字符串中找到指定的名称及其值?