JQuery与JSON数组 – 转换为Javascript数组

我从asp.net webservice获得以下XML输出:

1710171117121713Teleszkóp 350mm gázasTeleszkóp 150mm olajTeleszkóp 260mm olaj sárgaTeleszkóp 260mm első 

我正在使用JQuery的$ Ajax从服务器获取它,它工作正常。 它被转换为JSON对象,但是如何将其转换回Javascript数组呢?

更新 :问题是,如果用eval()解析,这个Array-in-Array只变成一个字符串!

那不是JSON对象:它是xml。 JSON本质上 javascript,看起来更像是这样:

[[“1710”,“1711”,“1712”,“1713”],[“Teleszkóp350mmgázas”,“Teleszkóp150mmolaj”,“Teleszkóp260mmolajsárga”,“Teleszkóp260mmelső”]]

我假设您的数据将返回并由jQuery自动解析并放入XML文档中。 这是将XML对象展平为数组的一种方法:

  my parsedData = []; $('result', data).each(function() { parsedData.push( { name: $('name', this).text(), addr: $('addr', this).text(), city: $('city', this).text(), state: $('state', this).text(), zip: $('zip', this).text() }); 
 var array = eval(json.d); 

其中array是javascript数组,json是json对象,json.d是json字符串。

那么这是我编写的用于将XML对象转换为本机JavaScript对象(包括数组)的代码。 你只需要打电话

 Object.fromXML(yourXMLObject) 

并且您将获得一个本机JavaScript对象,其JSON等效于此:

 { ArrayOfString: [ {string: ['1710', '1711', '1712', '1713']}, {string: ['Teleszkóp 350mm gázas', 'Teleszkóp 150mm olaj', 'Teleszkóp 260mm olaj sárga', 'Teleszkóp 260mm első']} ] } 

该函数的来源如下。

 /** * Tries to convert a given XML data to a native JavaScript object by traversing the DOM tree. * If a string is given, it first tries to create an XMLDomElement from the given string. * * @param {XMLDomElement|String} source The XML string or the XMLDomElement prefreably which containts the necessary data for the object. * @param {Boolean} [includeRoot] Whether the "required" main container node should be a part of the resultant object or not. * @return {Object} The native JavaScript object which is contructed from the given XML data or false if any error occured. */ Object.fromXML=function(source, includeRoot) { if (typeof source=='string') { try { if (window.DOMParser) source=(new DOMParser()).parseFromString(source, "application/xml"); else if (window.ActiveXObject) { var xmlObject=new ActiveXObject("Microsoft.XMLDOM"); xmlObject.async=false; xmlObject.loadXML(source); source=xmlObject; xmlObject=undefined; } else throw new Error("Cannot find an XML parser!"); } catch(error) { return false; } } var result={}; if (source.nodeType==9) source=source.firstChild; if (!includeRoot) source=source.firstChild; while (source) { if (source.childNodes.length) { if (source.tagName in result) { if (result[source.tagName].constructor != Array) result[source.tagName] = [result[source.tagName]]; result[source.tagName].push(Object.fromXML(source)); } else result[source.tagName] = Object.fromXML(source); } else if (source.tagName) result[source.tagName] = source.nodeValue; else result = source.nodeValue; source = source.nextSibling; } return result; }; 

如果它是JSON ,你不需要转换任何东西……例如:

 var jsonString = "....."; var converted = eval(jsonString); 

JSON表示JavaScript Object Notation,因此JSON格式的任何内容都可以直接在JavaScript中使用。

你有什么是XML。 您应该检查并手动转换为JavaScript。

如果你明确告诉jQuery你正在期待一个XML文档(使用dataType选项 ),或者你没有指定数据类型,并且服务器正在以XML格式正确地发送它(在这种情况下,jQuery会猜测并且给你回来responseXML而不是responseText ),你应该能够在你的成功回调函数中使用以下内容从XML中提取字符串数组数组,其中data是一个XML文档:

 $(data).find("ArrayOfString").map(function() { return $(this).find('string').map(function() { return $(this).text(); }); }); 

你的问题似乎与它的标题不匹配,但是我已经读了几次,我认为答案是,(在javascript中):

 var JSONstring = '{"something":"like this"}'; var newArray = JSON.parse(JSONstring); 

适用于Firefox 15.0.1

jQuery方式是:

  newArray = $.parseJSON(JSONstring);