使用JSON或XML发送JavaScript数组?如何将其转换为XML?

时间:2009-04-19 16:28:57

标签: php javascript xml json prototypejs

好的,我的情况是这样的:我需要将JavaScript数组的内容发送到服务器,其中PHP脚本将在MYSQL数据库中保存单个数组条目。我不是在使用jQuery,而是将Prototype框架集成到我的项目中。

我认为可以使用Prototype提供的toJSON()方法轻松地将JS数组转换为JSON。然后,我可以将此POST发送到我的脚本,该脚本将以某种方式对数组进行de-JSONise并将值插入到DB中。

但我感兴趣的不是使用JSON数据交换格式,而是将JS数组转换为XML,这可以通过simplexml PHP扩展非常容易地解析(节省自己一些开发时间服务器端)。因此,我的问题是:我应该选择JSON还是XML?我怎么能把JS数组变成XML? (是否存在toXML()方法,就像Prototype中的toJSON()一样?)

我知道各种非常相似的问题,但他们似乎都反过来问这个问题......将JSON转换为JS数组,并且许多都与jQuery相关。所以,请帮助我,即使这可能是重复的,你可能已经在其他地方回答了这个问题。

4 个答案:

答案 0 :(得分:4)

你试过php_json扩展方法吗?使用它们,您将能够将JSON对象转换为PHP对象。

从那里你可以做任何你想做的事。创建一个XML字符串以使用SimpleXML进行处理或持久保存到DataStore。

答案 1 :(得分:2)

如果你使用php的内置json_decode

,你将真正节省开发时间

答案 2 :(得分: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。

答案 3 :(得分: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。如果您是其中之一,您可以轻松更改该功能以作为单独的功能。如果需要帮助,请敲我;)