将 XML 字符串转换为 JSON 的工具 (javascript)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7769829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:32:52  来源:igfitidea点击:

Tool (javascript) to convert a XML string to JSON

javascriptxmljson

提问by David Laberge

What is the best javascript function/plugin/library to convert a XML string to JSON.

将 XML 字符串转换为 JSON 的最佳 javascript 函数/插件/库是什么。

I found that tool : http://www.thomasfrank.se/xml_to_json.html, but it does not like strings starting with 0. i.e.: 005321get converted to 2769(not cool :( )

我发现该工具:http: //www.thomasfrank.se/xml_to_json.html,但它不喜欢以0. 即:005321转换为2769(不酷:()

My question, what is the best javascript function/plugin/library to convert a XML to JSON?

我的问题是,将 XML 转换为 JSON 的最佳 javascript 函数/插件/库是什么?

EDIT : Someone tried one that works fine?

编辑:有人试过一种效果很好的吗?

回答by James Johnson

This function has worked pretty well for me:

这个功能对我来说效果很好:

xmlToJson = function(xml) {
    var obj = {};
    if (xml.nodeType == 1) {                
        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) { 
        obj = xml.nodeValue;
    }            
    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].push) == "undefined") {
                    var old = obj[nodeName];
                    obj[nodeName] = [];
                    obj[nodeName].push(old);
                }
                obj[nodeName].push(xmlToJson(item));
            }
        }
    }
    return obj;
}

Implementation:

执行:

var jsonText = JSON.stringify(xmlToJson(xmlDoc)); // xmlDoc = xml dom document

回答by abdolence

Another small library for XML <=> JSON is https://github.com/abdmob/x2js

另一个用于 XML <=> JSON 的小型库是https://github.com/abdmob/x2js

回答by Jared Farrish

If you're willing to use jQuery, there is:

如果你愿意使用 jQuery,有:

http://www.fyneworks.com/jquery/xml-to-json/

http://www.fyneworks.com/jquery/xml-to-json/

$.get("http://jfcoder.com/test.xml.php", function(xml){
    var json = $.xml2json(xml);
    $('pre').html(JSON.stringify(json)); // To show result in the browser
});

Using:

使用:

<nums>
 <num>00597</num>
 <num>0059</num>
 <num>5978</num>
 <num>5.978</num>
</nums>

Outputs:

输出:

{"num":["00597","0059","5978","5.978"]}

http://jfcoder.com/test.php

http://jfcoder.com/test.php