Python脚本将xml转换为json,但数组保持为数组,对象保持为对象

2024-05-16 03:15:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我的Python脚本将xml转换为json。但是for.g地址的数组被转换成我不想要的对象。我希望它只保留为数组。 下面是我的python脚本以及我期望的输入xml和输出json。 谢谢你的帮助 在我们的应用程序中,我们将xml作为变量中的输入字符串,并在变量中输出json,然后测试并比较xml和json

import json
import xmltodict

xml = xmltodict.parse(inputxml)
CustomerCreateandUpdate = xml['SyncCustomerCreateandUpdate']['DataArea']['CustomerCreateandUpdate']
outputjson = json.dumps(CustomerCreateandUpdate)

<?xml version="1.0" encoding="UTF-8"?>
<SyncCustomerCreateandUpdate xmlns="http://schema.infor.com/InforOAGIS/2" 
xmlns:xs="http://www.w3.org/2001/XMLSchema" languageCode="EN" releaseID="9.2" 
systemEnvironmentCode="Production" versionID="2.11.0">
<ApplicationArea>
    <Sender>
        <LogicalID schemeVersionID="16.0.0.015">lid://infor.m3.m3</LogicalID>
        <ComponentID schemeVersionID="16.0.0.20201209170022">M3BE</ComponentID>
        <ConfirmationCode>OnError</ConfirmationCode>
    </Sender>
    <CreationDateTime>2021-03-04T12:33:12.487Z</CreationDateTime>
    <BODID>1bb40f80-8039-4264-b903-fa58ca2d3a9a</BODID>
</ApplicationArea>
<DataArea>
    <Sync>
        <AccountingEntityID>001_090</AccountingEntityID>
        <ActionCriteria>
            <ActionExpression actionCode="Add"/>
        </ActionCriteria>
    </Sync>
    <CustomerCreateandUpdate>
        <customerName>TEST1838</customerName>
        <customerStatus>Test5</customerStatus>
        <addresses>
            <addressLine1>test</addressLine1>
            <addressLine2>test</addressLine2>
            <addressLine3>test</addressLine3>
            <addressLine4>test</addressLine4>
            <postalCode>2200</postalCode>
            <city>Herentals</city>
            <stateCode>test</stateCode>
            <countryCode>BE</countryCode>
        </addresses>
        <phones>
            <phoneNumber>test</phoneNumber>
        </phones>
        <faxNumber>999</faxNumber>
        <contacts>
            <firstName>test</firstName>
            <lastName>test</lastName>
            <emailAddress>test</emailAddress>
            <mobilePhone>test</mobilePhone>
        </contacts>
        <emails>
            <emailType>TEST</emailType>
            <emailAddress>Test</emailAddress>
        </emails>
        <references>test</references>
        <freeFields>
            <number>0</number>
            <content>test</content>
        </freeFields>
        <searchKey>test</searchKey>
        <customerType>test</customerType>
        <description>test</description>
        <serviceArea>test</serviceArea>
        <lelyCenter>test</lelyCenter>
        <lspId>test</lspId>
        <m3Id>test</m3Id>
        <createdById>test</createdById>
        <lastModifiedById>test</lastModifiedById>
        <lastModifiedDate>test</lastModifiedDate>
        <vatNumber>test</vatNumber>
    </CustomerCreateandUpdate>
</DataArea>
Json expected output:

{
"customerName": "",
"customerStatus": "",
"addresses": [
{
  "addressLine1": "",
  "addressLine2": "",
  "addressLine3": "",
  "addressLine4": "",
  "postalCode": "",
  "city": "",
  "stateCode": "",
  "countryCode": ""
  }
   ],
  "phones": [
  {
  "phoneNumber": ""
  }
  ],
  "faxNumber": "",
 "contacts": [
  {
  "firstName": "",
  "lastName": "",
  "emailAddress": "",
  "mobilePhone": ""
   }
   ],
  "emails": [
   {
   "emailType": "",
   "emailAddress": ""
   }
   ],
  "references": [
  ""
  ],
  "freeFields": [
  {
  "number": 0,
  "content": ""
  }
   ],
 "language": "",
 "searchKey": "",
 "customerType": 0,
 "description": "",
 "lelyCenter": "",
 "lspId": "",
 "m3Id": "",
 "createdById": "",
 "lastModifiedById": "",
 "lastModifiedDate": "",
 "vatNumber": ""
 }

Tags: testjsoncityaddressesxmlemailaddressdataareacustomername
1条回答
网友
1楼 · 发布于 2024-05-16 03:15:08

在xml驱动器中描述地址数据的方式xmltodict将其表示为json对象。首先,地址数据应该封装到一个专用容器中,比如(How to represent list data in XML):

  <addresses>

    <address>
      <addressLine1>test</addressLine1>
      <addressLine2>test</addressLine2>
      <addressLine3>test</addressLine3>
      <addressLine4>test</addressLine4>
      <postalCode>2200</postalCode>
      <city>Herentals</city>
      <stateCode>test</stateCode>
      <countryCode>BE</countryCode>
    </address>

  </addresses>

但显然,这种结构不足以让xmltodict将其检测为项目列表。它需要浏览同一标记至少到次,才能将其理解为一个iterable。因此,如果您的地址列表只有一个元素,则必须放置一个空地址标记:

  <addresses>

    <address>
      <addressLine1>test</addressLine1>
      <addressLine2>test</addressLine2>
      <addressLine3>test</addressLine3>
      <addressLine4>test</addressLine4>
      <postalCode>2200</postalCode>
      <city>Herentals</city>
      <stateCode>test</stateCode>
      <countryCode>BE</countryCode>
    </address>

    <address></address>
    
  </addresses>

编辑

对于新版本的xmltodict,无需添加void标记。只需在解析方法(xmltodict does not return a list for one element)中使用参数force_list={<;tag name>;}

xmltodict.parse(xml_data, force_list={"addresses"})

相关问题 更多 >