[英]JSON to XML Using Javascript
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 converting into XML as expected我正在尝试将 JSON 转换为 XML 但没有得到确切的输出。在我的 JSON 有数组对象它没有将它转换为 XML 数组。主要是数组对象没有按预期转换为 XML
var InputJSON = "{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}";
var output = eval("OBJtoXML("+InputJSON+");")
function OBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
xml += "<" + prop + ">";
if(obj[prop] instanceof Array) {
for (var array in obj[prop]) {
xml += OBJtoXML(new Object(obj[prop][array]));
}
} else if (typeof obj[prop] == "object") {
xml += OBJtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += "</" + prop + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g,'');
return xml
}
Actual Output:实际输出:
<body>
<entry>
<fullURL>abcd</fullURL>
<Resource>1234</Resource>
<fullURL>efgh</fullURL>
<Resource>5678</Resource>
</entry>
</body>
Expected Output:预期输出:
<body>
<entry>
<fullURL>abcd</fullURL>
<Resource>1234</Resource>
</entry>
<entry>
<fullURL>efgh</fullURL>
<Resource>5678</Resource>
</entry>
</body>
Please guide me if i am missing anything from the code to get my expected result如果我遗漏了代码中的任何内容以获得预期结果,请指导我
replace your OBJtoXML
function with将您的
OBJtoXML
函数替换为
function OBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
xml += obj[prop] instanceof Array ? '' : "<" + prop + ">";
if (obj[prop] instanceof Array) {
for (var array in obj[prop]) {
xml += "<" + prop + ">";
xml += OBJtoXML(new Object(obj[prop][array]));
xml += "</" + prop + ">";
}
} else if (typeof obj[prop] == "object") {
xml += OBJtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += obj[prop] instanceof Array ? '' : "</" + prop + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml
}
There are a few problems here, for starters, here the JSON string variable either needs to have it's quotes escaped.这里有一些问题,对于初学者来说,这里的 JSON 字符串变量要么需要对其引号进行转义。 Or be wrapped in single quotes.
或者用单引号括起来。 For example:
例如:
var InputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
Next, there is no need to use eval
here, when using JSON in JavaScript you should use JSON.parse
接下来,这里不需要使用
eval
,在 JavaScript 中使用 JSON 时应该使用JSON.parse
// First parse the JSON
var InputJSON = JSON.parse(InputJSON);
// Now execute the 'OBJtoXML' function
var output = OBJtoXML(InputJSON);
Now we come to the meat of this question, why is entry
only occuring once?现在我们来到这个问题的核心,为什么
entry
只发生一次? The problem that you're having is that xml += "<" + prop + ">";
您遇到的问题是
xml += "<" + prop + ">";
and xml += "</" + prop + ">";
和
xml += "</" + prop + ">";
are only happening once per property.每个属性只发生一次。 A possible solution would look like this:
一个可能的解决方案如下所示:
function OBJtoXML(obj) {
var xml = '';
for (var prop in obj) {
xml += "<" + prop + ">";
if(Array.isArray(obj[prop])) {
for (var array of obj[prop]) {
// A real botch fix here
xml += "</" + prop + ">";
xml += "<" + prop + ">";
xml += OBJtoXML(new Object(array));
}
} else if (typeof obj[prop] == "object") {
xml += OBJtoXML(new Object(obj[prop]));
} else {
xml += obj[prop];
}
xml += "</" + prop + ">";
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g,'');
return xml
}
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 += '</' + prop + '>';
}
} else {
xml += '<' + prop + '>';
typeof obj[prop] == 'object' ? xml += OBJtoXML(new Object(obj[prop])) : xml += obj[prop];
xml += '</' + prop + '>';
}
}
var xml = xml.replace(/<\/?[0-9]{1,}>/g, '');
return xml;
}
import { json2xml } from "xml-js";
const input = {
contact: {
name: `John & cia "example"`
}
};
const xml = json2xml(input, {
compact: true
});
// <contact><name>John & cia \"example\"</name></contact>
https://codesandbox.io/s/xml-json-forked-zgit4?file=/src/index.js:97-103 https://codesandbox.io/s/xml-json-forked-zgit4?file=/src/index.js:97-103
:) :)
Xml-to-json library has method jsonToXml(json)
. Xml-to-json库有方法
jsonToXml(json)
。
var inputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
var xml = jsonToXml(inputJSON);
// <?xml version="1.0" encoding="UTF-8"?>
// <body>
// <entry>
// <fullURL>abcd</fullURL>
// <Resource>1234</Resource>
// </entry>
// <entry>
// <fullURL>efgh</fullURL>
// <Resource>5678</Resource>
// </entry>
// </body>
function OBJtoXML(obj, index) {
var xml = '',
root,
count = 0;
if (index > 0) count = index;
for (var prop in obj) {
switch (typeof obj[prop]) {
case 'object': {
if(obj[prop] instanceof Array) {
for (var instance in obj[prop]) {
xml += `\n\t<${prop}>\n${OBJtoXML(new Object(obj[prop][instance]))}\t</${prop}>`;
}
}
else {
if (count === 0) {
// console.log(`Setting root: ${prop}`)
root = prop
}
xml += `<${prop}>${OBJtoXML(new Object(obj[prop]), count)}\n</${prop}>\n`;
}
break;
}
case 'number':
case 'string': {
// console.log(`Setting ${typeof obj[prop]}`)
xml += `\t\t<${prop}>${obj[prop]}</${prop}>\n`;
break;
}
}
count += 1;
}
return xml
}
var InputJSON = '{"body":{"entry": [{ "fullURL" : "abcd","Resource": "1234"},{ "fullURL" : "efgh","Resource": "5678"}]}}';
var output = eval("OBJtoXML("+InputJSON+");");
console.log(output);
const objectToXml = (object) => Object.keys(object).reduce((reqStr, key) => {
const value = object[key] || '';
const isObject = typeof value === 'object';
const isArray = Array.isArray(value);
if (isArray) {
return reqStr + value.reduce((accumulator, currentValue) =>
accumulator + `<${key}>${ typeof currentValue === 'object' ? objectToXml(currentValue) : (currentValue || '')}</${key}>`
,'');
}
if (isObject) {
return reqStr + `<${key}>${objectToXml(value)}</${key}>`;
}
return reqStr + `<${key}>${value}</${key}>`;
}, '');
const output = objectToXml(yourJson);
console.log(output);
The following solution can turn a JS variable to a (non indented) XML string.以下解决方案可以将 JS 变量转换为(非缩进)XML 字符串。 It supports both lists (arrays) and objects.
它支持列表(数组)和对象。
objectToXml(object)
to create a valid XML with a single root element.objectToXml(object)
创建具有单个根元素的有效 XML。variableToXml(variable, 'listElementName')
if you want to convert an array of elements.variableToXml(variable, 'listElementName')
。 In this case, you have to pass the tag name for the list elements as the second argument.function objectToXml(object) {
if (object instanceof Array || Object.keys(object).length !== 1) {
throw 'variable has to be an object with a single property'
}
return variableToXml(object)
}
function variableToXml(variable, arrayItemPropertyName = null) {
if (Array.isArray(variable)) {
return variable.reduce((xml, propertyValue) => {
const value = variableToXml(propertyValue)
return `${xml}<${arrayItemPropertyName}>${value}</${arrayItemPropertyName}>`
}, '')
}
if (variable instanceof Object) {
return Object.entries(variable).reduce((xml, [propertyName, propertyValue]) => {
const value = variableToXml(propertyValue, propertyName )
const tag = propertyValue instanceof Array ? value : `<${propertyName}>${value}</${propertyName}>`
return `${xml}${tag}`
}, '')
}
return variable
}
const object = {
rootTag: {
intProperty: 4,
stringProperty: 'foo',
listOfElements: {
element: [{
intProperty: 5,
stringProperty: 'bar',
}, {
intProperty: 5,
stringProperty: 'bar',
}],
},
list: {
listElement: [1, 2, 3],
},
},
}
<rootTag>
<intProperty>4</intProperty>
<stringProperty>foo</stringProperty>
<listOfElements>
<element>
<intProperty>5</intProperty>
<stringProperty>bar</stringProperty>
</element>
<element>
<intProperty>5</intProperty>
<stringProperty>bar</stringProperty>
</element>
</listOfElements>
<list>
<listElement>1</listElement>
<listElement>2</listElement>
<listElement>3</listElement>
</list>
</rootTag>
const object = { rootTag: { intProperty: 4, stringProperty: 'foo', listOfElements: { element: [{ intProperty: 5, stringProperty: 'bar', }, { intProperty: 5, stringProperty: 'bar', }], }, list: { listElement: [1, 2, 3], }, }, } console.log(objectToXml(object)) function objectToXml(object) { if (object instanceof Array || Object.keys(object).length !== 1) { throw 'variable has to be an object with a single property' } return variableToXml(object) } function variableToXml(variable, arrayItemPropertyName = null) { if (Array.isArray(variable)) { return variable.reduce((xml, propertyValue) => { const value = variableToXml(propertyValue) return `${xml}<${arrayItemPropertyName}>${value}</${arrayItemPropertyName}>` }, '') } if (variable instanceof Object) { return Object.entries(variable).reduce((xml, [propertyName, propertyValue]) => { const value = variableToXml(propertyValue, propertyName ) const tag = propertyValue instanceof Array ? value : `<${propertyName}>${value}</${propertyName}>` return `${xml}${tag}` }, '') } return variable }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.