Python 我需要从json创建一个漂亮的输出

Python 我需要从json创建一个漂亮的输出,python,json,dictionary,output,Python,Json,Dictionary,Output,我正在从active directory获取数据,并将其导出为JSON。太好了。然后,我可以通过json.loads()返回输出 然后我得到最终用户无法使用的丑陋json输出。我是否可以将其转换为Python字典,或者是否有其他模块可以在用户可接受的视图中显示数据 我尝试用Python JSON.decoder转换JSON 尝试对json.decode进行解码时出现以下错误: 类型错误:是 不可序列化 我试过这些建议,但不起作用。到目前为止,以下代码是唯一有效的交互,没有可用的格式: for s

我正在从active directory获取数据,并将其导出为JSON。太好了。然后,我可以通过json.loads()返回输出

然后我得到最终用户无法使用的丑陋json输出。我是否可以将其转换为Python字典,或者是否有其他模块可以在用户可接受的视图中显示数据

我尝试用Python JSON.decoder转换JSON

尝试对json.decode进行解码时出现以下错误:

类型错误:是 不可序列化


我试过这些建议,但不起作用。到目前为止,以下代码是唯一有效的交互,没有可用的格式:

for server in ADDomainList:
    counter += 1
    psCommand = 'get-ADUser ' + cdsid + ' -Server ' + server + ' -Properties * | 
                 SELECT custom1,custom2,custom3,custom4 | ConvertTo-Json'

    proc = subprocess.Popen(['powershell.exe ', psCommand] 
           stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    output = proc.stdout.read().decode('utf-8')

    if output != '':
       return json.loads(output)
    if counter > 5:
           return 'AD Server Cannot Be Located'
我得到:

“custom1”:“dummy1”,
“custom10”:“dummy10”,
“custom2”:“dummy2”,
“custom3”:“dummy3”,

我想返回:

字段名称1:Data1
字段名称10:Data10


没有引号,没有分隔符,只有事实…

json模块已经使用indent参数实现了一些基本的漂亮打印:

import json

json_ = '["foo", {"bar":["val1", "val2", "val3", "val4"]}]'
parsed = json.loads(json_)
print (json.dumps(parsed, indent=2, sort_keys=True))
输出:

[
  "foo",
  {
    "bar": [
      "val1",
      "val2",
      "val3",
      "val4"
    ]
  }
]
缩进:要缩进的空格数(没有缩进,只会得到一行)

该模块提供了以可作为解释器输入的形式“漂亮地打印”任意Python数据结构的功能。下面是一个如何使用它漂亮地打印JSON对象的示例

import json
import pprint
from urllib.request import urlopen
with urlopen('http://pypi.python.org/pypi/configparser/json') as url:
    http_info = url.info()
    raw_data = url.read().decode(http_info.get_content_charset())
project_info = json.loads(raw_data)
result = {'headers': http_info.items(), 'body': project_info}
pprint.pprint(result)
输出:

{'body': {'info': {'_pypi_hidden': False,
                   '_pypi_ordering': 12,
                   'classifiers': ['Development Status :: 4 - Beta',
                                   'Intended Audience :: Developers',
                                   'License :: OSI Approved :: MIT License',
                                   'Natural Language :: English',
                                   'Operating System :: OS Independent',
                                   'Programming Language :: Python',
                                   'Programming Language :: Python :: 2',
                                   'Programming Language :: Python :: 2.6',
                                   'Programming Language :: Python :: 2.7',
                                   'Topic :: Software Development :: Libraries',
                                   'Topic :: Software Development :: Libraries :: Python Modules'],
                   'download_url': 'UNKNOWN',
                   'home_page': 'http://docs.python.org/py3k/library/configparser.html',
                   'keywords': 'configparser ini parsing conf cfg configuration file',
                   'license': 'MIT',
                   'name': 'configparser',
                   'package_url': 'http://pypi.python.org/pypi/configparser',
                   'platform': 'any',
                   'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
                   'requires_python': None,
                   'stable_version': None,
                   'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
                   'version': '3.2.0r3'},
        'urls': [{'comment_text': '',
                  'downloads': 47,
                  'filename': 'configparser-3.2.0r3.tar.gz',
                  'has_sig': False,
                  'md5_digest': '8500fd87c61ac0de328fc996fce69b96',
                  'packagetype': 'sdist',
                  'python_version': 'source',
                  'size': 32281,
                  'upload_time': '2011-05-10T16:28:50',
                  'url': 'http://pypi.python.org/packages/source/c/configparser/configparser-3.2.0r3.tar.gz'}]},
'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
            ('Server', 'Apache/2.2.16 (Debian)'),
            ('Content-Disposition', 'inline'),
            ('Connection', 'close'),
            ('Transfer-Encoding', 'chunked'),
            ('Content-Type', 'application/json; charset="UTF-8"')]}

如果您的json是一个
dict
对象(这是
加载的
所做的),请使用
print(json.dumps(my_dict,indent=4))
或使用pretty print:
https://docs.python.org/3.6/library/pprint.html
导入pprint.pprint(json)
我尝试了这些建议,但不起作用。到目前为止,以下代码是唯一有效的交互,并且没有可用的格式:谢谢,我相信pprint是最好的选择。除此之外,我将把它发送到Web前端,使其对用户友好。
{'body': {'info': {'_pypi_hidden': False,
                   '_pypi_ordering': 12,
                   'classifiers': ['Development Status :: 4 - Beta',
                                   'Intended Audience :: Developers',
                                   'License :: OSI Approved :: MIT License',
                                   'Natural Language :: English',
                                   'Operating System :: OS Independent',
                                   'Programming Language :: Python',
                                   'Programming Language :: Python :: 2',
                                   'Programming Language :: Python :: 2.6',
                                   'Programming Language :: Python :: 2.7',
                                   'Topic :: Software Development :: Libraries',
                                   'Topic :: Software Development :: Libraries :: Python Modules'],
                   'download_url': 'UNKNOWN',
                   'home_page': 'http://docs.python.org/py3k/library/configparser.html',
                   'keywords': 'configparser ini parsing conf cfg configuration file',
                   'license': 'MIT',
                   'name': 'configparser',
                   'package_url': 'http://pypi.python.org/pypi/configparser',
                   'platform': 'any',
                   'release_url': 'http://pypi.python.org/pypi/configparser/3.2.0r3',
                   'requires_python': None,
                   'stable_version': None,
                   'summary': 'This library brings the updated configparser from Python 3.2+ to Python 2.6-2.7.',
                   'version': '3.2.0r3'},
        'urls': [{'comment_text': '',
                  'downloads': 47,
                  'filename': 'configparser-3.2.0r3.tar.gz',
                  'has_sig': False,
                  'md5_digest': '8500fd87c61ac0de328fc996fce69b96',
                  'packagetype': 'sdist',
                  'python_version': 'source',
                  'size': 32281,
                  'upload_time': '2011-05-10T16:28:50',
                  'url': 'http://pypi.python.org/packages/source/c/configparser/configparser-3.2.0r3.tar.gz'}]},
'headers': [('Date', 'Sat, 14 May 2011 12:48:52 GMT'),
            ('Server', 'Apache/2.2.16 (Debian)'),
            ('Content-Disposition', 'inline'),
            ('Connection', 'close'),
            ('Transfer-Encoding', 'chunked'),
            ('Content-Type', 'application/json; charset="UTF-8"')]}