首页 > 解决方案 > 用python将XML转换为JSON,JSON中子节点后面的父属性

问题描述

我需要使用 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,我只需要将节点父亲的属性更改为最终。

在某些网站上制作我需要的内容: https ://www.convertjson.com/xml-to-json.htm

还有一个不怎么样:

http://www.utilities-online.info/xmltojson/#.X_crINhKiUk

有人可以帮助我吗?

标签: pythonjsonxmlxmltodict

解决方案


我可以使用https://pypi.org/project/xmljson/库进行格式化,我修改了 BadgerFish 样式

class BadgerFish(XMLData): # 约定从项目中改变了前缀 _ 并排序父节点的属性 '''使用 BadgerFish 约定在 XML 和数据之间转换''' def init (self, **kwargs): super (獾鱼,自我)。初始化(attr_prefix='_', text_content='$', **kwargs)

我将属性的前缀更改为“_”

by 否则只改了**# modify 把父属性改完,怎么看代码

def data(self, root):
        '''Convert etree.Element into a dictionary'''
        value = self.dict()
        children = [node for node in root if isinstance(node.tag, basestring)]
  
        if root.text and self.text_content is not None:
            text = root.text
            if text.strip():
                if self.simple_text and len(children) == len(root.attrib) == 0:
                    value = self._fromstring(text)
                else:
                    value[self.text_content] = self._fromstring(text)
        count = Counter(child.tag for child in children)
        for child in children:
            # if child.tag == "System_State_Entry": print(child.tag)
            if count[child.tag] == 1:
                value.update(self.data(child))
            else:
                result = value.setdefault(child.tag, self.list())
                result += self.data(child).values()
        # if simple_text, elements with no children nor attrs become '', not {}
        if isinstance(value, dict) and not value and self.simple_text:
            value = ''

        **# modify to put the father atributes to finish
        for attr, attrval in root.attrib.items():
            attr = attr if self.attr_prefix is None else self.attr_prefix + attr
            value[attr] = self._fromstring(attrval)**     
        return self.dict([(root.tag, value)])

推荐阅读