15

我有一个大字典,里面有一些大数组数据:

d = {'something': {'else': 'x'}, 'longnumbers': [1,2,3,4,54,6,67,7,7,8,8,8,6,4,3,3,5,6,7,4,3,5,6,54]}

真正的字典有更多的键和嵌套结构。当我使用json.dumpwithoutindent时,我得到一个不可读的紧凑单行输出。当我设置indent时,它会在每个分隔符之后放置换行符,包括数组。

数值数组很长,最终如下所示:

  "longnumbers": [
    1, 
    2, 
    3, 
    4, 
    54, 
    6, 
    67, 
    7, 
    7, 
    8, 
    8, 
    8, 
    6, 
    4, 
    3, 
    3, 
    5, 
    6, 
    7, 
    4, 
    3, 
    5, 
    6, 
    54
  ], 

有什么办法可以得到带有缩进级别的漂亮打印的 JSON,但不在数组元素之后放置换行符?对于上面的例子,我想要这样的东西:

{
  "longnumbers": [1, 2, 3, 4, 54, 6, 67, 7, 7, 8, 8, 8, 6, 4, 3, 3, 5, 6, 7, 4, 3, 5, 6, 54],
  "something": {
    "else": "x"
  }
}
4

3 回答 3

14

我最终只是编写了自己的 JSON 序列化程序:

import numpy

INDENT = 3
SPACE = " "
NEWLINE = "\n"

def to_json(o, level=0):
    ret = ""
    if isinstance(o, dict):
        ret += "{" + NEWLINE
        comma = ""
        for k,v in o.iteritems():
            ret += comma
            comma = ",\n"
            ret += SPACE * INDENT * (level+1)
            ret += '"' + str(k) + '":' + SPACE
            ret += to_json(v, level + 1)

        ret += NEWLINE + SPACE * INDENT * level + "}"
    elif isinstance(o, basestring):
        ret += '"' + o + '"'
    elif isinstance(o, list):
        ret += "[" + ",".join([to_json(e, level+1) for e in o]) + "]"
    elif isinstance(o, bool):
        ret += "true" if o else "false"
    elif isinstance(o, int):
        ret += str(o)
    elif isinstance(o, float):
        ret += '%.7g' % o
    elif isinstance(o, numpy.ndarray) and numpy.issubdtype(o.dtype, numpy.integer):
        ret += "[" + ','.join(map(str, o.flatten().tolist())) + "]"
    elif isinstance(o, numpy.ndarray) and numpy.issubdtype(o.dtype, numpy.inexact):
        ret += "[" + ','.join(map(lambda x: '%.7g' % x, o.flatten().tolist())) + "]"
    elif o is None:
        ret += 'null'
    else:
        raise TypeError("Unknown type '%s' for json serialization" % str(type(o)))
    return ret
于 2012-05-02T19:05:01.323 回答
4

呃,现在应该是为两种不同的 JSON 容器类型指定不同缩进的选项。如果您想与核心 Python JSON 库保持兼容,另一种方法是覆盖该_make_iterencode()库中负责处理的函数(当前)indent

重新实现 _make_iterencode() 时遇到了问题。只需更改几即可indent选择,可选地采用 tuple (hash-indent, array-indent)。但不幸的是,必须更换一个整体_make_iterencode(),结果证明它相当大且分解不良。无论如何,以下适用于 3.4-3.6:

import sys
import json

dat = {"b": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], "a": 1, "c": "x"}
indent = 2
print(json.dumps(dat, indent=indent))

if sys.version_info.major == 3 and 4 <= sys.version_info.minor <= 6:
  import _make_iterencode
  json.encoder._make_iterencode = _make_iterencode._make_iterencode
  indent = (2, None)

print(json.dumps(dat, indent=indent))

给出

{
  "c": "x",
  "a": 1,
  "b": [
    1,
    2,
    3,
    4,
    5,
    6,
    7,
    8,
    9,
    10
  ]
}
{
  "c": "x",
  "a": 1,
  "b": [1,2,3,4,5,6,7,8,9,10]
}
于 2018-08-16T01:51:38.953 回答
3

@jterrace 的答案是为 Python 2 编写的,它已被 Python 3 弃用,并更改了类型。因此,由于他的回答得到了应有的认可,我对其进行了一些调整以供我个人使用并与 Python 3 兼容,包括支持将元组作为列表:

import numpy

INDENT = 3
SPACE = " "
NEWLINE = "\n"

# Changed basestring to str, and dict uses items() instead of iteritems().
def to_json(o, level=0):
  ret = ""
  if isinstance(o, dict):
    ret += "{" + NEWLINE
    comma = ""
    for k, v in o.items():
      ret += comma
      comma = ",\n"
      ret += SPACE * INDENT * (level + 1)
      ret += '"' + str(k) + '":' + SPACE
      ret += to_json(v, level + 1)

    ret += NEWLINE + SPACE * INDENT * level + "}"
  elif isinstance(o, str):
    ret += '"' + o + '"'
  elif isinstance(o, list):
    ret += "[" + ",".join([to_json(e, level + 1) for e in o]) + "]"
  # Tuples are interpreted as lists
  elif isinstance(o, tuple):
    ret += "[" + ",".join(to_json(e, level + 1) for e in o) + "]"
  elif isinstance(o, bool):
    ret += "true" if o else "false"
  elif isinstance(o, int):
    ret += str(o)
  elif isinstance(o, float):
    ret += '%.7g' % o
  elif isinstance(o, numpy.ndarray) and numpy.issubdtype(o.dtype, numpy.integer):
    ret += "[" + ','.join(map(str, o.flatten().tolist())) + "]"
  elif isinstance(o, numpy.ndarray) and numpy.issubdtype(o.dtype, numpy.inexact):
    ret += "[" + ','.join(map(lambda x: '%.7g' % x, o.flatten().tolist())) + "]"
  elif o is None:
    ret += 'null'
  else:
    raise TypeError("Unknown type '%s' for json serialization" % str(type(o)))
  return ret
于 2020-07-29T20:45:45.113 回答