[英]Convert XML to nested JSON object in specific format
这是我之前提出的关于从XML节点中获取完全平面结构的问题的后续跟进: 将xml doc转换为特定的点扩展json结构 。
假设我有相同的XML开头:
<Item ID="288917">
<Main>
<Platform>iTunes</Platform>
<PlatformID>353736518</PlatformID>
</Main>
<Genres>
<Genre FacebookID="6003161475030">Comedy</Genre>
<Genre FacebookID="6003172932634">TV-Show</Genre>
</Genres>
<Products>
<Product Country="CA">
<URL>https://itunes.apple.com/ca/tv-season/id353187108?i=353736518</URL>
<Offers>
<Offer Type="HDBUY">
<Price>3.49</Price>
<Currency>CAD</Currency>
</Offer>
<Offer Type="SDBUY">
<Price>2.49</Price>
<Currency>CAD</Currency>
</Offer>
</Offers>
</Product>
<Product Country="FR">
<URL>https://itunes.apple.com/fr/tv-season/id353187108?i=353736518</URL>
<Rating>Tout public</Rating>
<Offers>
<Offer Type="HDBUY">
<Price>2.49</Price>
<Currency>EUR</Currency>
</Offer>
<Offer Type="SDBUY">
<Price>1.99</Price>
<Currency>EUR</Currency>
</Offer>
</Offers>
</Product>
</Products>
</Item>
现在我想将它转换为特定格式的嵌套json对象(与xmltodict
库略有不同。这是我想要派生的结构:
{
"Item[@ID]": 288917,
"Item.Main.Platform": "iTunes",
"Item.Main.PlatformID": "353736518",
"Item.Genres": [
{
"[@FacebookID]": "6003161475030",
"Value": "Comedy"
},
{
"[@FacebookID]": "6003161475030",
"Value": "TV-Show"
}
],
"Item.Products": [
{
"[@Country]": "CA",
"URL": "https://itunes.apple.com/ca/tv-season/id353187108?i=353736518",
"Offers.Offer": [
{
"[@Type]": "HDBUY",
"Price": "3.49",
"Currency": "CAD"
}
{
"[@Type]": "SDBUY",
"Price": "2.49",
"Currency": "CAD"
}
]
},
{
"[@Country]": "FR",
"URL": "https://itunes.apple.com/fr/tv-season/id353187108?i=353736518",
"Offers.Offer": [
{
"[@Type]": "HDBUY",
"Price": "3.49",
"Currency": "EUR"
}
{
"[@Type]": "SDBUY",
"Price": "1.99",
"Currency": "EUR"
}
]
}
]
}
主要区别在于不是将所有内容都折叠成平面值列表,而是允许列表。 怎么可以这样做?
虽然执行上述操作可能是一个很好的挑战,但xmltodic
已经做得很好,可以轻微改变。
以下是xmltodict
中的更改:
#text
更改为Value
。 @
更改为[@
。 attr_suffix=']'
到init方法。 key = self.attr_prefix+self._build_name(key)+self.attr_suffix
。 这应该给出您正在寻找的测试模块的确切结果:
>>> from lxml import etree
>>> import xmltodict
>>> import json
>>> from utils import xmltodict
>>> node= etree.fromstring(s)
>>> d=xmltodict.parse(etree.tostring(node))
>>> print(json.dumps(d, indent=4))
{
"Item": {
"[@ID]": "288917",
"Main": {
"Platform": "iTunes",
"PlatformID": "353736518"
},
"Genres": {
"Genre": [
{
"[@FacebookID]": "6003161475030",
"Value": "Comedy"
},
{
"[@FacebookID]": "6003172932634",
"Value": "TV-Show"
}
]
},
"Products": {
"Product": [
{
"[@Country]": "CA",
"URL": "https://itunes.apple.com/ca/tv-season/id353187108?i=353736518",
"Offers": {
"Offer": [
{
"[@Type]": "HDBUY",
"Price": "3.49",
"Currency": "CAD"
},
{
"[@Type]": "SDBUY",
"Price": "2.49",
"Currency": "CAD"
}
]
}
},
{
"[@Country]": "FR",
"URL": "https://itunes.apple.com/fr/tv-season/id353187108?i=353736518",
"Rating": "Tout public",
"Offers": {
"Offer": [
{
"[@Type]": "HDBUY",
"Price": "2.49",
"Currency": "EUR"
},
{
"[@Type]": "SDBUY",
"Price": "1.99",
"Currency": "EUR"
}
]
}
}
]
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.