我需要从json中创建输出

2024-06-01 00:39:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在从active directory获取数据,并将其导出为JSON。太好了。然后我可以通过json.loads文件()。你知道吗

if output != '':
            return json.loads(output)

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

我试着用Pythonjson解码器

我在尝试json.decode地址:

TypeError: is not JSON serializable

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

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:数据1
字段名10:Data10

没有引号,没有分隔符,只有事实。。。你知道吗


Tags: 数据jsonoutputreturnifservercountersubprocess
2条回答

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"
    ]
  }
]

indent: the number of spaces to indent by (Without the indent, you just get a single line)

pprint模块提供了一种以可作为解释器输入的形式“漂亮地打印”任意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"')]}

相关问题 更多 >