亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何強制 xml2js 為某些鍵輸出數組?

如何強制 xml2js 為某些鍵輸出數組?

UYOU 2021-11-25 16:11:37
我有一些 XML,我希望它輸出到一個數組中。但是當只有一項存在時,xml2js 會將一項輸出到一個對象。當有多個項目時,它輸出為一個數組。為了一致性,我如何強制它成為一個數組?我的 XML 看起來像這樣:const xml = `<Shift>    <Transaction>      <Currency>GBP</Currency>      <Amount>24.50</Amount>    </Transaction></Shift>`parseStringPromise(xml).then(json => console.log(JSON.stringify(json)))您可以看到輸出為:{  "Transaction": {    "Currency": "GBP",    "Amount": "24.50"  }}但通常有多個事務。因此,通常 XML 將如下所示:const xml = `<Shift>    <Transaction>      <Currency>GBP</Currency>      <Amount>24.50</Amount>    </Transaction>    <Transaction>      <Currency>GBP</Currency>      <Amount>25.50</Amount>    </Transaction>    <Transaction>      <Currency>GBP</Currency>      <Amount>26.50</Amount>    </Transaction>    <Transaction>      <Currency>GBP</Currency>      <Amount>27.50</Amount>    </Transaction></Shift>`parseStringPromise(xml).then(json => console.log(JSON.stringify(json)))它像這樣輸出到一個數組中{  "Transaction": [    {      "Currency": "GBP",      "Amount": "24.50"    },    {      "Currency": "GBP",      "Amount": "25.50"    },    {      "Currency": "GBP",      "Amount": "26.50"    },    {      "Currency": "GBP",      "Amount": "27.50"    }  ]}我怎樣才能確保即使Transactionxml 中只有 1 個項目,它也會輸出到一個數組?是否有需要傳遞給解析器的選項,或者是否需要重新格式化我的 XML?
查看完整描述

2 回答

?
茅侃侃

TA貢獻1842條經驗 獲得超22個贊

為此,您可以使用camaro。camaro 允許您編寫一個基于 xpath 的模板來指定您想要的輸出方式。在這種情況下,你指定你想要一個數組


const { transform } = require('camaro')

const xml = `<Shift>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>24.50</Amount>

    </Transaction>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>25.50</Amount>

    </Transaction>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>26.50</Amount>

    </Transaction>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>27.50</Amount>

    </Transaction>

</Shift>`


;(async function () {

    const template = {

        transactions: ['/Shift/Transaction', {

            Currency: 'Currency',

            Amount: 'Amount'

        }]

    }


    console.log(await transform(xml, template));


})()

輸出


{ transactions:

   [ { Currency: 'GBP', Amount: '24.50' },

     { Currency: 'GBP', Amount: '25.50' },

     { Currency: 'GBP', Amount: '26.50' },

     { Currency: 'GBP', Amount: '27.50' } ] }

即使只有 1 個元素


const { transform } = require('../')

const xml = `<Shift>

    <Transaction>

      <Currency>GBP</Currency>

      <Amount>24.50</Amount>

    </Transaction>

</Shift>`


;(async function () {

    const template = {

        transactions: ['/Shift/Transaction', {

            Currency: 'Currency',

            Amount: 'Amount'

        }]

    }


    console.log(await transform(xml, template));


})()

輸出


{ transactions: [ { Currency: 'GBP', Amount: '24.50' } ] }


查看完整回答
反對 回復 2021-11-25
?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

有很多 XML 到 JSON 轉換器,它們都有這種問題。它們在某些情況下會做您想做的事,而在其他情況下則不會,而且它們都沒有提供太多控制權。如果您想控制生成的 JSON,我建議使用 XSLT 3.0:這允許您在調用xml-to-json()進行轉換之前將 XML 轉換為與您想要生成的 JSON 的結構相對應。


查看完整回答
反對 回復 2021-11-25
  • 2 回答
  • 0 關注
  • 222 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號