2009-04-19 31 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。如果你是他们中的一员,你可以改变这个功能来轻松地作为一个独立的功能。如果需要帮助,只需敲我;)