嗨我有这样的xml -
这是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)
我用正则表达式对此进行了编码。如果需要,您可以添加一些额外的检查。
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){
var result = {};
var re = new RegExp("Column([0-9]+)Agg"+i+"='([0-9.-]+)'", "g"), m, p = 0;
do {
m = re.exec(search);
if (m) {
result["Column" + m[1] + "Agg" + i] = m[2];
p++;
}
} while (m);
if(p>0){
result.children = getChildren(i+1, search);
}
return result;
}
var m;
var results = {};
results = getChildren(1, data);
results.Month = /Month='([^']+)'/g.exec(data)[1];
results.children.Week = /Week='([^']+)'/g.exec(data)[1];
results.children.children.Date = /Date='([^']+)'/g.exec(data)[1];
console.log(results);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
答案 1 :(得分: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="."/>