Jequery对json数据的处理

Jequery对json的处理程序

//加载时绑定事件
$(document).ready(function(){
 //把对象转化为json数据 button单价事件
 $("#toJSON").click( function () {
   var obj = {
       name : "sean",
       friend : ["fans","bruce","wawa"],
       action : function(){alert("gogogog")},
       boy  : true,
       age : 26,
       reg : /\b([a-z]+) \1\b/gi,
       child : {
     name : "none",
     age : -1
       }
      };
     
      var json = $.toJSON(obj);
      alert(json);
      var objx = $.evalJSON(json); 
      alert(objx.name);
 });
 $("#orid").change(function () {
  var obj = $("# orid");
  var str = document.getElementById("orid").value;
  alert(str);
 });
 
 
 
});

jQuery.extend(
 {
  /**
   * @see  将json字符串转换为对象
   * @param   json字符串
   * @return 返回object,array,string等对象
   */
  evalJSON : function (strJson)
  {
   return eval( "(" + strJson + ")");
  }
 });
 jQuery.extend(
 {
  /**
   * @see  将javascript数据类型转换为json字符串
   * @param 待转换对象,支持object,array,string,function,number,boolean,regexp
   * @return 返回json字符串
   */
  toJSON : function (object)
  {
   var type = typeof object;
   if ('object' == type)
   {
    if (Array == object.constructor)
     type = 'array';
    else if (RegExp == object.constructor)
     type = 'regexp';
    else
     type = 'object';
   }
      switch(type)
   {
         case 'undefined':
       case 'unknown':
     return;
     break;
    case 'function':
       case 'boolean':
    case 'regexp':
     return object.toString();
     break;
    case 'number':
     return isFinite(object) ? object.toString() : 'null';
       break;
    case 'string':
     return '"' + object.replace(/(\\|\")/g,"\\$1").replace(/\n|\r|\t/g,
       function(){  
                 var a = arguments[0];                  
        return  (a == '\n') ? '\\n':  
                       (a == '\r') ? '\\r':  
                       (a == '\t') ? '\\t': "" 
             }) + '"';
     break;
    case 'object':
     if (object === null) return 'null';
        var results = [];
        for (var property in object) {
          var value = jQuery.toJSON(object[property]);
          if (value !== undefined)
            results.push(jQuery.toJSON(property) + ':' + value);
        }
        return '{' + results.join(',') + '}';
     break;
    case 'array':
     var results = [];
        for(var i = 0; i < object.length; i++)
     {
      var value = jQuery.toJSON(object[i]);
           if (value !== undefined) results.push(value);
     }
        return '[' + results.join(',') + ']';
     break;
      }
  }
 });

function ReloadDivisions(deptid){
 var sel = document.getElementById("orid");
 
 //getsubcategory(deptvalue);
  $.ajax(
          {
            url: "getsubcategory.asp",
            type: "get",
            data: "deptid="+deptid,
            success: function(json)
            {
    //var oXMLDocument = originalRequest.responseXML.documentElement;
    //for(var i=0;i<oXMLDocument.childNodes.length;i++)
     //{
      //addSelectElement(sel,
      //    oXMLDocument.childNodes[i].childNodes[0].nodeValue,    //option控件文本
      //    oXMLDocument.childNodes[i].attributes[0].value        //option控件值
      //    )
    // }
     //使当前的父分类的select控件可
     var selectDiv = $("#orid");
     //清空插件
     selectDiv.empty();
     //追加固定项
     selectDiv.append("<option value='' selected>All Divisions</option>");
     //var jsonarry = $.toJSON(json);
     var objx = eval( "([" + json + "])");
     var arryList = new Array();
     arryList = objx;
     for(var i = 0;i<arryList.length-1;i++){
      //alert(objx[i].name+"");
      
      selectDiv.append("<option value='"+objx[i].id+"'>"+objx[i].name+"</option>"); 
     }
     
     //alert(json);
     //var userlist = $.parseJSON(json); 
     //alert(userlist);
     $("#test").html(""+json);
              
            },
   error:function()
   { 
    alert("def");
   }
            });
}

function changge(date){
 alert(date);
}

posted @ 2010-03-03 16:05  why520crazy  阅读(838)  评论(0编辑  收藏  举报