2009-04-15 47 views
2

我從一個asp.net web服務的XML輸出如下:jQuery的使用JSON陣列 - 轉換爲JavaScript數組

<ArrayOfArrayOfString><ArrayOfString><string>1710</string><string>1711</string><string>1712</string><string>1713</string></ArrayOfString><ArrayOfString><string>Teleszkóp 350mm gázas</string><string>Teleszkóp 150mm olaj</string><string>Teleszkóp 260mm olaj sárga</string><string>Teleszkóp 260mm első</string></ArrayOfString></ArrayOfArrayOfString> 

我使用jQuery的$阿賈克斯從服務器得到它,它工作正常。 它已轉換爲JSON對象,但我怎樣才能將它轉換回Javascript數組?

更新:問題是,如果使用eval()分析它,則此Array-in-Array僅成爲一個字符串!

回答

5

這不是JSON對象:它是xml。 JSON本質 JavaScript和看起來更像是這樣的:

[ 「1710」, 「1711」, 「1712」, 「1713」],[ 「Teleszkóp350毫米gázas」,「Teleszkóp150毫米olaj 」, 「Teleszkóp260毫米olajsárga」, 「Teleszkóp260毫米ELSO」]]

+0

這是真的,這是神奇地通過web服務JSON化;但在JS中,我只看到[Object object] :) – balint 2009-04-15 15:47:43

+0

[Object object]是您的數據 - 您可以像訪問數組一樣訪問它。 – btk 2010-07-24 03:47:01

2

我假設你的數據回來並被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() 
     }); 
0

如果是JSON,你就不需要任何轉換...如:

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

JSON,這就意味着JavaScript對象符號,所以無論是在JSON格式直接在JavaScript中工作。

你在那裏有XML。您應該瀏覽它並手動轉換爲JavaScript。

2
var array = eval(json.d); 

其中array是javascript數組,json是json對象,json.d是json字符串。

2

那麼這裏有一個代碼,我已經寫了一個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; 
}; 
0

如果你告訴你期待一個XML文檔背面(使用dataType option),或者如果你沒有指定的數據類型和服務器反正正確發送它作爲XML(以明確的jQuery這種情況下,jQuery會猜測並返回responseXML而不是responseText),那麼您應該能夠在成功回調函數中使用以下內容從XML抽取字符串數組,其中data是XML文檔:

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

你的問題似乎與它的標題不太匹配,但哈維NG讀了幾次,我認爲這個問題的答案是,(在JavaScript):

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

在Firefox 15.0.1

jQuery的方法是:

newArray = $.parseJSON(JSONstring);