【发布时间】:2019-07-01 15:35:57
【问题描述】:
我正在从活动目录中获取数据,并将其导出为 JSON。那太棒了。然后我可以通过 json.loads() 返回输出。
if output != '':
return json.loads(output)
然后我得到丑陋的 json 输出,最终用户无法使用。我可以将其转换为 Python 字典,还是有其他模块可以在用户可接受的视图中显示数据?
我尝试用 Python json.decoder
转换 JSON尝试 json.decode 时出现以下错误:
类型错误:是 不可 JSON 序列化
我已经尝试了这些建议,但它不起作用。迄今为止,以下代码是唯一有效的交互,没有可用的格式:
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
没有引号,没有分隔符,只有事实......
【问题讨论】:
-
如果您的 json 是
dict对象(这是loads所做的),请使用print(json.dumps(my_dict, indent=4)) -
或者你可以使用漂亮的打印:
https://docs.python.org/3.6/library/pprint.html -
import pprint pprint.pprint(json) -
我已经尝试了这些建议,但它不起作用。迄今为止,以下代码是唯一有效的交互,并且没有可用的格式:
标签: python json dictionary output