2017-04-11 80 views
0

您好我有這樣的XML -轉換XML的成JSON

這是一個基於XML

<item col_sort='20160102' Month='Jan-2016' Week='Week 1' Date='01/02/2016' Name='A' Id='1' 
        Column1='0' Column1Agg1='1410.00' Column1Agg2='405.00' Column1Agg3='0' 
        Column2='38' Column2Agg1='2976' Column2Agg2='79' Column2Agg3='79' 
        Column3='0.32' Column3Agg1='0.56' Column3Agg2='0.41' Column3Agg3='0.41' 
        Column4='11.42' Column4Agg1='8.48' Column4Agg2='9.55' Column4Agg3='9.55' 
        Column5='8.76' Column5Agg1='2.97' Column5Agg2='9.81' Column5Agg3='9.81' 
        Column6='-1' Column6Agg1='-2' Column6Agg2='-1' Column6Agg3='-1' 
        Column7='-99' Column7Agg1='99.9' Column7Agg2='-99' Column7Agg3='-99' 
        Column8='-99.00' Column8Agg1='8.00' Column8Agg2='-99.00' Column8Agg3='-99.00' 
        Column9='17.82' Column9Agg1='9.96' Column9Agg2='17.84' Column9Agg3='17.84' 
        Column10='18.18' Column10Agg1='10.51' Column10Agg2='18.28' Column10Agg3='18.28' 
        Column11='-2' Column11Agg1='-2' Column11Agg2='-2' Column11Agg3='-2' 
        Column12='7.9' Column12Agg1='44.6' Column12Agg2='7.6' Column12Agg3='7.6' 
        Column13='12.86' Column13Agg1='10.48' Column13Agg2='12.68' Column13Agg3='12.68' /> 

的節點現在,這裏的AGG的前綴的子節點。因此,在一個節點中,我們讓所有的孩子都與父母一起重視。這裏的Agg1代表父母。 Agg2是Agg1的孩子,Agg3是Agg2的孩子。沒有前綴的節點是葉節點。

json = [ { 
     "Month": "Dec-2016", 
     "Column1Agg1": "1410.00", 
     "Column2Agg1": "2976", 
     "Column3Agg1": "0.56", 
     "Column4Agg1": "8.48",, 
     "Column5Agg1": "2.97", 
     "Column6Agg1": "-2", 
     "Column7Agg1": "-99.9", 
     "Column8Agg1": "8.00", 
     "Column9Agg1": "9.96", 
     "Column10Agg1": "10.51", 
     "Column11Agg1": "-2", 
     "Column12Agg1": "44.6", 
     "Column13Agg1": "10.48", 
     "children" : [ 
     {"Week": "Week 1", 
     "Column1Agg2": "405.00", 
     "Column2Agg2": "79", 
     "Column3Agg2": "0.41", 
     "Column4Agg2": "9.55", 
     "Column5Agg2": "9.81", 
     "Column6Agg2": "-1", 
     "Column7Agg2": "-99", 
     "Column8Agg2": "-99.00", 
     "Column9Agg2": "17.84", 
     "Column10Agg2": "18.28", 
     "Column11Agg2": "-2", 
     "Column12Agg2": "7.6", 
     "Column13Agg2": "12.68", 
     "children" : [{ 
      "Date": "01/02/2016", 
     "Column1Agg3": "405.00", 
     "Column2Agg3": "79", 
     "Column3Agg3": "0.41", 
     "Column4Agg3": "9.55", 
     "Column5Agg3": "9.81", 
     "Column6Agg3": "-1", 
     "Column7Agg3": "-99", 
     "Column8Agg3": "-99.00", 
     "Column9Agg3": "17.84", 
     "Column10Agg3": "18.28", 
     "Column11Agg3": "-2", 
     "Column12Agg3": "7.6", 
     "Column13Agg3": "12.68", 
     "children" : [{ 
     "Name" : "A", 
     "Id" : "1", 
     "Column1": "0", 
     "Column2": "38", 
     "Column3": "0.32", 
     "Column4": "11.42", 
     "Column5": "8.76", 
     "Column6": "-1", 
     "Column7": "-99", 
     "Column8": "-99.00", 
     "Column9": "17.82", 
     "Column10": "18.18", 
     "Column11": "-2", 
     "Column12": "7.9", 
     "Column13": "12.86" 

      }] 
      }] 
      }] 
      }] 

如何將這種類型的xml轉換爲帶有子項的json。

任何想法如何做到這一點

+0

請同時添加一個問題。你描述過什麼,你有什麼問題? –

+0

打開google並輸入「javascript xml to json」並點擊第一個鏈接。 –

+0

@TahaPaksu它不會創建子節點,它只會轉換...我想創建子節點 – shv22

回答

0

我用正則表達式編碼。如果需要,您可以添加一些額外的檢查。

var data = "<item col_sort='20160102' Month='Jan-2016' Week='Week 1' Date='01/02/2016' Name='A' Id='1' \ 
 
        Column1='0' Column1Agg1='1410.00' Column1Agg2='405.00' Column1Agg3='0' \ 
 
        Column2='38' Column2Agg1='2976' Column2Agg2='79' Column2Agg3='79' \ 
 
        Column3='0.32' Column3Agg1='0.56' Column3Agg2='0.41' Column3Agg3='0.41' \ 
 
        Column4='11.42' Column4Agg1='8.48' Column4Agg2='9.55' Column4Agg3='9.55'\ 
 
        Column5='8.76' Column5Agg1='2.97' Column5Agg2='9.81' Column5Agg3='9.81'\ 
 
        Column6='-1' Column6Agg1='-2' Column6Agg2='-1' Column6Agg3='-1'\ 
 
        Column7='-99' Column7Agg1='99.9' Column7Agg2='-99' Column7Agg3='-99'\ 
 
        Column8='-99.00' Column8Agg1='8.00' Column8Agg2='-99.00' Column8Agg3='-99.00'\ 
 
        Column9='17.82' Column9Agg1='9.96' Column9Agg2='17.84' Column9Agg3='17.84' \ 
 
        Column10='18.18' Column10Agg1='10.51' Column10Agg2='18.28' Column10Agg3='18.28'\ 
 
        Column11='-2' Column11Agg1='-2' Column11Agg2='-2' Column11Agg3='-2'\ 
 
        Column12='7.9' Column12Agg1='44.6' Column12Agg2='7.6' Column12Agg3='7.6' \ 
 
        Column13='12.86' Column13Agg1='10.48' Column13Agg2='12.68' Column13Agg3='12.68' />"; 
 

 
    var getChildren = function(i, search){ 
 
\t \t var result = {}; 
 
\t  var re = new RegExp("Column([0-9]+)Agg"+i+"='([0-9.-]+)'", "g"), m, p = 0; \t \t \t  
 
\t \t do { 
 
\t \t  m = re.exec(search); \t \t  \t \t  
 
\t \t  if (m) { \t \t  \t 
 
\t \t   result["Column" + m[1] + "Agg" + i] = m[2]; 
 
\t \t   p++; 
 
\t \t  } 
 
\t \t } while (m); \t \t \t 
 
    \t \t if(p>0){ 
 
\t \t \t result.children = getChildren(i+1, search); 
 
\t \t } 
 
\t \t return result; 
 
\t } 
 

 
    var m; 
 
\t var results = {}; 
 
\t 
 
\t results = getChildren(1, data); \t 
 
\t results.Month = /Month='([^']+)'/g.exec(data)[1]; 
 
\t results.children.Week = /Week='([^']+)'/g.exec(data)[1]; 
 
\t results.children.children.Date = /Date='([^']+)'/g.exec(data)[1]; 
 
\t 
 
\t console.log(results);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

+0

謝謝你......但是有些誤解Column1Agg1是父項,Column1是葉節點但在這裏結果是我認爲是相反的 – shv22

+0

你能修正你的例子嗎?那麼我會修改我的答案。 –

+0

爲什麼你在每行之前添加了'/'......現在怎麼能在我的反應中做到這一點? – shv22

0

如果我這樣做,我會被你的瘋狂XML輸入轉換爲更衛生,沿着線開始:

<item col_sort='20160102' Month='Jan-2016' Week='Week 1' Date='01/02/2016' Name='A' Id='1'> 
    <column nr='1' value='0'> 
    <agg nr='1'>1410.00</agg> 
    <agg nr='2'>405.00</agg> 
    <agg nr='3'>0</agg> 
    </column> 
    <column nr='2' value='38'> 
    <agg nr='1'>2976</agg> 
    <agg nr='2'>79</agg> 
    <agg nr='3'>79</agg> 
    </column> 
etc 

這是用XSLT容易做得不夠。這裏有一個草圖:

<xsl:for-each-group select="@*" group-by="f:colNr(name())"> 
    <xsl:sort select="number(current-grouping-key())"/> 
    <column nr="{current-grouping-key()}"> 
    <xsl:for-each select="current-group()"> 
     <xsl:sort select="f:aggNr(name())"> 
     <agg nr="{f:aggNr(name())}"> 
     <xsl:value-of select="."/>