【发布时间】:2021-01-07 15:50:15
【问题描述】:
我需要使用python将一个文件XML转换为JSON,但我需要将父亲的属性放在JSON中的子节点之后
我现在的代码是这样的。
def generate_json(self, event=None):
# opening the xml file
with open(self.fn ,"r") as xmlfileObj:
data_dict = lib.xmltodict.parse(xmlfileObj.read(),attr_prefix='_')
xmlfileObj.close()
jsonObj= json.dumps(data_dict, sort_keys=False)
restored = json.loads(jsonObj)
#storing json data to json file
with open("data.json", "w") as jsonfileObj:
jsonfileObj.write(jsonObj)
jsonfileObj.close()
我需要这个;
{
"datasetVersion": {
"metadataBlocks": {
"citation": {
"fields": [
{
"value": "Darwin's Finches",
"typeClass": "primitive",
"multiple": false,
"typeName": "title"
}
],
"displayName": "Citation Metadata"
}
}
}
}
代替:
{
"datasetVersion": {
"metadataBlocks": {
"citation": {
"displayName": "Citation Metadata",
"fields": [
{
"value": "Darwin's Finches",
"typeClass": "primitive",
"multiple": false,
"typeName": "title"
}
]
}
}
}
}
不按字母顺序改变sort_keys=False,我只需要将节点father的属性改为final即可。
在某些网站上制作我需要的内容: https://www.convertjson.com/xml-to-json.htm
还有一个不怎么样:
http://www.utilities-online.info/xmltojson/#.X_crINhKiUk
有人可以帮我吗?
【问题讨论】:
-
顺序真的重要吗?它不应该适用于大多数应用程序。
-
是的,因为从这个方案中可以使用先前开发的较低级别的子系统:/
-
AFAIK json 对象按标准是无序的,因此除非您使用自定义 json 解析器或在子系统上进行一些验证,否则无论如何都无关紧要。