我有一个 XML 网络服务响应(见下文),并且想在不知道每个键的情况下将其转换为 JSON。
响应要大得多,这只是显示结构的示例。
这可以在 Groovy 中实现吗?
<allMortProdContainers>
<WsMortProdContainerv02>
<allWsMortProdCapCollarBandByEnd xsi:nil="true"/>
<allWsMortProdCashBackBandByEnd xsi:nil="true"/>
<allWsMortProdEarlyRepaymentBandByEnd>
<WsMortProdEarlyRepaymentBandv01>
<endDate>???</endDate>
<endMonth>???</endMonth>
<fixedCharge>???</fixedCharge>
<monthsInterest>???</monthsInterest>
<percentage>???</percentage>
</WsMortProdEarlyRepaymentBandv01>
</allWsMortProdEarlyRepaymentBandByEnd>
<myWsMortProdSpec>
<productBaseRate>???</productBaseRate>
<productBaseRateDescription>???</productBaseRateDescription>
<productCode>???</productCode>
<productDescription>???</productDescription>
<productDescriptionWebFriendly>???</productDescriptionWebFriendly>
<productInfoKey>???</productInfoKey>
<productType>???</productType>
</myWsMortProdSpec>
</WsMortProdContainerv02>
</allMortProdContainers>
最佳答案
@Grab(group='org.json', module='json', version='20180130')
import org.json.XML;
def xml = '''<allMortProdContainers xmlns:xsi="...." >
<WsMortProdContainerv02>
<allWsMortProdCapCollarBandByEnd xsi:nil="true"/>
<allWsMortProdCashBackBandByEnd xsi:nil="true"/>
<allWsMortProdEarlyRepaymentBandByEnd>
<WsMortProdEarlyRepaymentBandv01>
<endDate>???</endDate>
<endMonth>???</endMonth>
<fixedCharge>???</fixedCharge>
<monthsInterest>???</monthsInterest>
<percentage>???</percentage>
</WsMortProdEarlyRepaymentBandv01>
</allWsMortProdEarlyRepaymentBandByEnd>
<myWsMortProdSpec>
<productBaseRate>???</productBaseRate>
<productBaseRateDescription>???</productBaseRateDescription>
<productCode>???</productCode>
<productDescription>???</productDescription>
<productDescriptionWebFriendly>???</productDescriptionWebFriendly>
<productInfoKey>???</productInfoKey>
<productType>???</productType>
</myWsMortProdSpec>
</WsMortProdContainerv02>
</allMortProdContainers>'''
println XML.toJSONObject(xml).toString(2)
关于json - Groovy - 在不知道每个键的情况下将 XML 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50838938/