JSON to XML Using Javascript

前端 未结 6 942
南旧
南旧 2020-12-11 09:25

I am trying to convert the JSON to XML but not getting exact output.In My JSON having array object it not converting that to XML array.Mainly array object is not converti

6条回答
  •  温柔的废话
    2020-12-11 10:18

    var inputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
    var parsedInput = JSON.parse(inputJSON);
    
    function OBJtoXML(obj) {
        var xml = '';
        for (var prop in obj) {
            if (obj[prop] instanceof Array) {
                for (var array in obj[prop]) {
                    xml += '<' + prop + '>';
                    xml += OBJtoXML(new Object(obj[prop][array]));
                    xml += '';
                }
            } else {
                xml += '<' + prop + '>';
                typeof obj[prop] == 'object' ? xml += OBJtoXML(new Object(obj[prop])) : xml += obj[prop];
                xml += '';
            }
        }
        var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
        return xml;
    }
    

提交回复
热议问题