[英]Parsing a nested JSON(?) object in Python
I use zeep to call a webservice.我使用 zeep 调用网络服务。
response = proces.service.Load(**params2, _soapheaders={'Header': header_value})
This returns an object which looks like this这将返回一个 object,如下所示
{
'LoadResult': None,
'hierarchy': {
'Code': 'FTE',
'Name': 'Balans en Winst & verlies',
'Description': None,
'RootNode': {
'Id': 757,
'Code': 'FTE',
'Name': 'Balans en Winst & verlies',
'Description': None,
'Accounts': None,
'ChildNodes': {
'HierarchyNode': [
{
'Id': 758,
'Code': '000',
'Name': 'Immateriële vaste activa',
'Description': None,
'Accounts': None,
'ChildNodes': {
'HierarchyNode': [
{
'Id': 759,
'Code': '00010',
'Name': 'Goodwill',
'Description': None,
'Accounts': {
'HierarchyAccount': [
{
'Type': 'BAS',
'Code': '0100',
'BalanceType': 'Balance'
},
{
'Type': 'BAS',
'Code': '0105',
'BalanceType': 'Balance'
}
]
},
'ChildNodes': None,
'Messages': None,
'Touched': 173
}
]
},
'Messages': None,
'Touched': 173
},
{
'Id': 760,
'Code': '010',
'Name': 'Materiële vaste activa',
'Description': None,
'Accounts': None,
'ChildNodes': {
'HierarchyNode': [
{
'Id': 761,
'Code': '01010',
'Name': 'Bedrijfsgebouwen en -terreinen',
'Description': None,
'Accounts': {
'HierarchyAccount': [
{
'Type': 'BAS',
'Code': '0090',
'BalanceType': 'Balance'
},
{
'Type': 'BAS',
'Code': '0110',
'BalanceType': 'Balance'
},
{
'Type': 'BAS',
'Code': '0115',
'BalanceType': 'Balance'
},
{
'Type': 'BAS',
'Code': '0120',
'BalanceType': 'Balance'
},
{
'Type': 'BAS',
'Code': '0125',
'BalanceType': 'Balance'
}
]
},
'ChildNodes': None,
'Messages': None,
'Touched': 173
},
{
'Id': 762,
'Code': '01020',
'Name': 'Machines en installaties',
'Description': None,
'Accounts': {
etc.等等
I want this hierarchy for a report.我想要这个层次结构的报告。 I want the resulting table to look something like
我希望结果表看起来像
So from left to right starting at the lowest hierarchy.所以从左到右从最低层次开始。
How can I best achieve this?我怎样才能最好地做到这一点? It is not just a straightforward json response.
这不仅仅是一个简单的 json 响应。 It says it cannot be serialized.
它说它不能被序列化。
and和
I read that the JSON should not have single quotes.我读到 JSON 不应该有单引号。 When I try to correct that:
当我尝试纠正时:
Jsonlint says this, when pasting the string corrected with double quotes Jsonlint 说这个,当粘贴用双引号纠正的字符串时
The JSON has already been parsed by the API library, and it constructed a hierarchy of objects that can be accessed using attributes. JSON 已经被 API 库解析,它构建了可以使用属性访问的对象层次结构。 The class also apparently provides its own
__repr__()
method that makes it look like a hierarchy of dictionaries; class 显然也提供了自己的
__repr__()
方法,使它看起来像字典的层次结构; but it's not actually dictionaries, so you can't use ['Attribute']
syntax.但它实际上不是字典,所以你不能使用
['Attribute']
语法。
If you want to loop through the HierarchyNode
list you could use如果你想遍历
HierarchyNode
列表,你可以使用
for node in response.hierarchy.RootNode.ChildNodes.HierarchyNode:
print(node.Id, node.Name, node.Code)
Got it to work using让它使用
for node in response.hierarchy.RootNode.ChildNodes.HierarchyNode:
if(hasattr(node.ChildNodes, 'HierarchyNode')):
for nood in node.ChildNodes.HierarchyNode:
if(hasattr(nood.Accounts, 'HierarchyAccount')):
for noodje in nood.Accounts.HierarchyAccount:
print(noodje.Code, noodje.Type,node.Id,node.Name,nood.Name, nood.Code)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.