提问人:BiswajitPaloi 提问时间:8/17/2023 最后编辑:mzjnBiswajitPaloi 更新时间:8/17/2023 访问量:114
使用命名空间将 JSON 转换为 XML
Converting JSON to XML with namespaces
问:
我的JSON数据:
{
"Type":"Baggage",
"TotalPrice":"INR10080",
"SupplierCode":"AI",
"CreateDate":"2023-08-16T06:29:51.961+00:00",
"ServiceStatus":"Offered",
"SequenceNumber":"1204",
"ServiceSubCode":"0C2",
"SSRCode":"XBAG",
"IssuanceReason":"C",
"Key":"PNmYlnTqWDKA0ie2FAAAAA==",
"AssessIndicator":"MileageOrCurrency",
"InclusiveOfTax":"true",
"InterlineSettlementAllowed":"false",
"GeographySpecification":"Sector",
"Source":"MCE",
"ViewableOnly":"false",
"TotalWeight":"20KG",
"ProviderCode":"1G",
"Quantity":"1",
"BasePrice":"INR9600",
"ApproximateTotalPrice":"INR10080",
"ApproximateBasePrice":"INR9600",
"Taxes":"INR480",
"IsRepriceRequired":"false",
"common_v52_0:ServiceData":{
"BookingTravelerRef":"PNmYlnTqWDKAvie2FAAAAA==",
"AirSegmentRef":"dnVZlnUqWDKAXC/qFAAAAA==",
"TravelerType":"ADT",
"common_v52_0:CabinClass":{
"Type":"Economy"
}
},
"common_v52_0:ServiceInfo":{
"common_v52_0:Description":"UPTO44LB_20KG_BAGGAGE"
},
"air:TaxInfo":{
"Category":"K3",
"Amount":"INR480",
"Key":"PNmYlnTqWDKA3ie2FAAAAA=="
},
"air:EMD":{
"FulfillmentType":"2",
"AssociatedItem":"Flight",
"RefundReissueIndicator":"NonRefundable",
"Commissionable":"false",
"Booking":"SSR",
"FulfillmentTypeDescription":"Associated_to_a_flight_coupon_of_a_ticket"
},
"air:FeeApplication":{
"Code":"4",
"#text":"Per_travel"
}
}
我想要这样的 XML:
<air:OptionalService Type="Baggage" TotalPrice="INR10080" SupplierCode="AI"
CreateDate="2023-08-16T06:29:51.961+00:00" ServiceStatus="Offered"
SequenceNumber="1204" ServiceSubCode="0C2" SSRCode="XBAG" IssuanceReason="C"
Key="PNmYlnTqWDKA0ie2FAAAAA==" AssessIndicator="MileageOrCurrency"
InclusiveOfTax="true" InterlineSettlementAllowed="false"
GeographySpecification="Sector" Source="MCE" ViewableOnly="false"
TotalWeight="20KG" ProviderCode="1G" Quantity="1" BasePrice="INR9600"
ApproximateTotalPrice="INR10080" ApproximateBasePrice="INR9600" Taxes="INR480"
IsRepriceRequired="false">
<common_v52_0:ServiceData BookingTravelerRef="PNmYlnTqWDKAvie2FAAAAA=="
AirSegmentRef="dnVZlnUqWDKAXC/qFAAAAA==" TravelerType="ADT">
<common_v52_0:CabinClass Type="Economy" />
</common_v52_0:ServiceData>
<common_v52_0:ServiceInfo>
<common_v52_0:Description>UPTO44LB 20KG BAGGAGE</common_v52_0:Description>
</common_v52_0:ServiceInfo>
<air:TaxInfo Category="K3" Amount="INR480" Key="PNmYlnTqWDKA3ie2FAAAAA==" />
<air:EMD FulfillmentType="2" AssociatedItem="Flight"
RefundReissueIndicator="NonRefundable" Commissionable="false" Booking="SSR"
FulfillmentTypeDescription="Associated to a flight coupon of a ticket" />
<air:FeeApplication Code="4">Per travel</air:FeeApplication>
</air:OptionalService>
我的代码:
import xml.etree.ElementTree as ET
def json_to_xml(element, data):
for key, value in data.items():
if isinstance(value, dict):
sub_element = ET.SubElement(element, key)
json_to_xml(sub_element, value)
else:
if ":" in key:
ns_prefix, local_name = key.split(":")
print(ns_prefix, local_name)
sub_element = ET.SubElement(element, "{" + ns_prefix + "}" + local_name)
sub_element.text = value
if key == "#text":
element.text = value
else:
element.set(key, value)
root = ET.Element("air:OptionalService")
json_to_xml(root, json_data)
tree = ET.ElementTree(root)
tree.write("test.xml", encoding="utf-8", xml_declaration=True)
这是输出:
<?xml version='1.0' encoding='utf-8'?>
<air:OptionalService xmlns:ns0="common_v52_0" Type="Baggage" TotalPrice="INR10080" SupplierCode="AI"
CreateDate="2023-08-16T06:29:51.961+00:00" ServiceStatus="Offered" SequenceNumber="1204"
ServiceSubCode="0C2" SSRCode="XBAG" IssuanceReason="C" Key="PNmYlnTqWDKA0ie2FAAAAA=="
AssessIndicator="MileageOrCurrency" InclusiveOfTax="true" InterlineSettlementAllowed="false"
GeographySpecification="Sector" Source="MCE" ViewableOnly="false" TotalWeight="20KG"
ProviderCode="1G" Quantity="1" BasePrice="INR9600" ApproximateTotalPrice="INR10080"
ApproximateBasePrice="INR9600" Taxes="INR480" IsRepriceRequired="false">
<common_v52_0:ServiceData BookingTravelerRef="PNmYlnTqWDKAvie2FAAAAA=="
AirSegmentRef="dnVZlnUqWDKAXC/qFAAAAA==" TravelerType="ADT">
<common_v52_0:CabinClass Type="Economy" />
</common_v52_0:ServiceData>
<common_v52_0:ServiceInfo common_v52_0:Description="UPTO44LB_20KG_BAGGAGE">
<ns0:Description>UPTO44LB_20KG_BAGGAGE</ns0:Description>
</common_v52_0:ServiceInfo>
<air:TaxInfo Category="K3" Amount="INR480" Key="PNmYlnTqWDKA3ie2FAAAAA==" />
<air:EMD FulfillmentType="2" AssociatedItem="Flight" RefundReissueIndicator="NonRefundable"
Commissionable="false" Booking="SSR"
FulfillmentTypeDescription="Associated_to_a_flight_coupon_of_a_ticket" />
<air:FeeApplication Code="4">Per_travel</air:FeeApplication>
</air:OptionalService>
一切都很好,但问题出在我的输出中,看看我输出中的第二行 - ,它不在我的 json 中。还有一个问题是在我的输出中它是不对的,看看我的json它不是它。xmlns:ns0="common_v52_0"
<ns0:Description>
"ns0:Description"
"common_v52_0:Description"
答:
0赞
antocabu
8/17/2023
#1
更改这部分代码
if "common_v52_0:Description" in key:
ns_prefix, local_name = key.split(":")
print(ns_prefix, local_name)
sub_element = ET.SubElement(element, "{" + ns_prefix + "}" + local_name)
sub_element.text = value
对此
if "common_v52_0:Description" in key:
sub_element = ET.SubElement(element, key)
sub_element.text = value
这将是结果
<?xml version='1.0' encoding='utf-8'?>
<air:OptionalService Type="Baggage" TotalPrice="INR10080" SupplierCode="AI" CreateDate="2023-08-16T06:29:51.961+00:00" ServiceStatus="Offered" SequenceNumber="1204" ServiceSubCode="0C2" SSRCode="XBAG" IssuanceReason="C" Key="PNmYlnTqWDKA0ie2FAAAAA==" AssessIndicator="MileageOrCurrency" InclusiveOfTax="true" InterlineSettlementAllowed="false" GeographySpecification="Sector" Source="MCE" ViewableOnly="false" TotalWeight="20KG" ProviderCode="1G" Quantity="1" BasePrice="INR9600" ApproximateTotalPrice="INR10080" ApproximateBasePrice="INR9600" Taxes="INR480" IsRepriceRequired="false">
<common_v52_0:ServiceData BookingTravelerRef="PNmYlnTqWDKAvie2FAAAAA==" AirSegmentRef="dnVZlnUqWDKAXC/qFAAAAA==" TravelerType="ADT">
<common_v52_0:CabinClass Type="Economy"/>
</common_v52_0:ServiceData>
<common_v52_0:ServiceInfo common_v52_0:Description="UPTO44LB_20KG_BAGGAGE">
<common_v52_0:Description>UPTO44LB_20KG_BAGGAGE</common_v52_0:Description>
</common_v52_0:ServiceInfo>
<air:TaxInfo Category="K3" Amount="INR480" Key="PNmYlnTqWDKA3ie2FAAAAA=="/>
<air:EMD FulfillmentType="2" AssociatedItem="Flight" RefundReissueIndicator="NonRefundable" Commissionable="false" Booking="SSR" FulfillmentTypeDescription="Associated_to_a_flight_coupon_of_a_ticket"/>
<air:FeeApplication Code="4">Per_travel</air:FeeApplication>
</air:OptionalService>
评论
1赞
mzjn
8/17/2023
结果 XML 格式不正确,因为没有 和 命名空间的声明。air
common_v52_0
0赞
BiswajitPaloi
8/21/2023
@antocabu这里一切都很好,但问题是:- .我不需要这个,因为我的json是,'“common_v52_0:ServiceInfo”:{ “common_v52_0:Description”:“UPTO44LB_20KG_BAGGAGE” },'<common_v52_0:ServiceInfo common_v52_0:Description="UPTO44LB_20KG_BAGGAGE"> <common_v52_0:Description>UPTO44LB_20KG_BAGGAGE</common_v52_0:Description> </common_v52_0:ServiceInfo>
common_v52_0:Description="UPTO44LB_20KG_BAGGAGE"
评论
air
common_v52_0