问题描述
我需要使用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","typeName": "title"
}
]
}
}
}
}
不用按字母顺序改sort_keys=False,我只需要把父节点的属性改成final即可。
在一些网站上制作我需要的: https://www.convertjson.com/xml-to-json.htm
还有一个没有办法:
http://www.utilities-online.info/xmltojson/#.X_crINhKiUk
有人可以帮我吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)