我现在有xml格式数据,要转换成json
xml: <a p="5" t="0" u="9" ce="0" count="1" fount="100"> 但是转换后是这样,
json: {"@e":"5","@t":"0","@u":"9","@ce":"0","@count":"1","@fount":"100"}如何不带“@”符号,我是使用这个方法转换的,JSON json = (JSON) new XMLSerializer().read(buf.toString());有没有其他好的方法,谢谢!
xml: <a p="5" t="0" u="9" ce="0" count="1" fount="100"> 但是转换后是这样,
json: {"@e":"5","@t":"0","@u":"9","@ce":"0","@count":"1","@fount":"100"}如何不带“@”符号,我是使用这个方法转换的,JSON json = (JSON) new XMLSerializer().read(buf.toString());有没有其他好的方法,谢谢!
function xmlToJson(xml) {
// Create the return object
var obj = {};
if (xml.nodeType == 1) { // element
// do attributes
if (xml.attributes.length > 0) {
obj["@attributes"] = {};
for ( var j = 0; j < xml.attributes.length; j++) {
var attribute = xml.attributes.item(j);
obj["@attributes"][attribute.nodeName] = attribute.nodeValue;
}
}
} else if (xml.nodeType == 3) { // text
obj = xml.nodeValue;
} // do children
if (xml.hasChildNodes()) {
for ( var i = 0; i < xml.childNodes.length; i++) {
var item = xml.childNodes.item(i);
var nodeName = item.nodeName;
if (typeof (obj[nodeName]) == "undefined") {
obj[nodeName] = xmlToJson(item);
} else {
if (typeof (obj[nodeName].length) == "undefined") {
var old = obj[nodeName];
obj[nodeName] = [];
obj[nodeName].push(old);
}
obj[nodeName].push(xmlToJson(item));
}
}
}
return obj;
};
有没有java代码,这是js的