2009-04-19 34 views
2

好吧,我的情況是這樣的:我需要將JavaScript數組的內容發送到服務器,PHP腳本會將單個數組條目保存在MYSQL數據庫中。我不使用jQuery,但我正在將Prototype框架集成到我的項目中。使用JSON或XML發送JavaScript數組?如何把它變成XML?

我注意到,使用Prototype提供的toJSON()方法可以輕鬆地將JS數組轉換爲JSON。然後,我可以將這個POST到我的腳本,它會以某種方式對數組取消JSON並將值插入到數據庫中。

但是我也感興趣的不是使用JSON數據交換格式,而是將JS數組轉換爲XML,這可以通過simplexml PHP擴展很容易解析(保存自己的一些開發時間服務器端)。因此,我的問題是:我應該使用JSON還是XML?以及如何將JS數組轉換爲XML? (是否有toXML()方法就像原型中的toJSON()?)

我意識到很多很類似的問題,但它們似乎都以相反的方式提出這個問題...轉換JSON到JS數組,許多都與jQuery相關。所以請幫助我,即使這可能是重複的,您可能已經在其他地方回答了這個問題。

回答

4

您試過php_json擴展方法嗎?使用它們,您將能夠將JSON對象變成一個PHP對象。

從那裏你可以做任何你想做的事情。使用SimpleXML處理XML字符串或保存到DataStore。

1

使用的toJSON(),你可以使用它來JSON轉換爲XML,從goessner.netsource file)後:

/* This work is licensed under Creative Commons GNU LGPL License. 

    License: http://creativecommons.org/licenses/LGPL/2.1/ 
    Version: 0.9 
    Author: Stefan Goessner/2006 
    Web:  http://goessner.net/ 
*/ 
function json2xml(o, tab) { 
    var toXml = function(v, name, ind) { 
     var xml = ""; 
     if (v instanceof Array) { 
     for (var i=0, n=v.length; i<n; i++) 
      xml += ind + toXml(v[i], name, ind+"\t") + "\n"; 
     } 
     else if (typeof(v) == "object") { 
     var hasChild = false; 
     xml += ind + "<" + name; 
     for (var m in v) { 
      if (m.charAt(0) == "@") 
       xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\""; 
      else 
       hasChild = true; 
     } 
     xml += hasChild ? ">" : "/>"; 
     if (hasChild) { 
      for (var m in v) { 
       if (m == "#text") 
        xml += v[m]; 
       else if (m == "#cdata") 
        xml += "<![CDATA[" + v[m] + "]]>"; 
       else if (m.charAt(0) != "@") 
        xml += toXml(v[m], m, ind+"\t"); 
      } 
      xml += (xml.charAt(xml.length-1)=="\n"?ind:"") + "</" + name + ">"; 
     } 
     } 
     else { 
     xml += ind + "<" + name + ">" + v.toString() + "</" + name + ">"; 
     } 
     return xml; 
    }, xml=""; 
    for (var m in o) 
     xml += toXml(o[m], m, ""); 
    return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, ""); 
} 

這就是說,我會親自去JSON。

+0

這看起來有點...繁瑣。首先轉換爲JSON,然後轉換爲XML ...我喜歡JSON的簡單性,我寧願去JSON,就像你說的那樣。在0..10的等級上,情況在PHP方面會有多複雜?如果我通過它JSON,我可以像使用simplexml解析XML一樣輕鬆地將JSON化對象轉換爲PHP對象嗎? – 2009-04-19 16:53:49

+1

是的,它非常笨重。但它(應該)工作。另外,我剛剛發現可能感興趣的http://us.php.net/json_decode。 – nsdel 2009-04-19 17:00:09

0

我建議使用本機查詢字符串,這將消除所有的皈依過程中你會真正地節省開發時間。這裏是將做適當轉換的代碼:

/** 
* This function serializes the object to a standart URI query string which can directly interpreted by PHP. 
* 
* @param {String} [format] The desired format for the output. Not needed for most usages. 
* @return {String} The URI query string. 
    */ 
Object.prototype.toQueryString=function(format, encodeURI) 
{ 
    if (typeof format!='string') 
     format='%s'; 
    var result=''; 
    for (var paramName in this) 
    { 
     if (this.constructor==Array && isNaN(parseInt(paramName)) || !this.hasOwnProperty(paramName)) 
      continue; 

     if (this[paramName].constructor==Object || this[paramName].constructor==Array) 
      result += '&' + this[paramName].toQueryString(format.format(paramName) + '[%s]', encodeURI); 
     else 
      result += '&' + format.format(paramName) + '=' + ((encodeURI!==false)?encodeURIComponent(this[paramName]):this[paramName]); 
    } 
    return result.substr(1); 
}; 

有些人可能不喜歡使用Object.prototype。如果你是他們中的一員,你可以改變這個功能來輕鬆地作爲一個獨立的功能。如果需要幫助,只需敲我;)