JS对象序列化为JSON对象

JS对象序列化为JSON对象,如下代码:


  1. /**  
  2.  * JSON 解析类  
  3.  * 方法:  
  4.  * 将JSON字符串解码为页面可识别的object对象  
  5.  * @param {String} json The JSON string  
  6.  * @return {Object} The resulting object   
  7.  * Object o = JSONUtil.decode(json);  
  8.  *   
  9.  * 将JS对象序列化为JSON字符串  
  10.  * @param {Mixed} o The variable to decode  
  11.  * @return {String} The JSON string  
  12.  * String json = JSONUtil.encode(o);  
  13.  */ 
  14.  
  15. var JSONUtil;  
  16. if (!JSONUtil) {  
  17.     JSONUtil = {};  
  18. }  
  19. JSONUtil.decode = function(json) {  
  20.     try {  
  21.         return eval("\u0028" + json + '\u0029');  
  22.     } catch (exception) {  
  23.         return eval("\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064");  
  24.     }  
  25. };  
  26. JSONUtil.encode = (function() {  
  27.     var $ = !!{}.hasOwnProperty, _ = function($) {  
  28.         return $ < 10 ? "0" + $ : $  
  29.     }, A = {  
  30.         "\b" : "\\b",  
  31.         "\t" : "\\t",  
  32.         "\n" : "\\n",  
  33.         "\f" : "\\f",  
  34.         "\r" : "\\r",  
  35.         "\"" : "\\\"",  
  36.         "\\" : "\\\\"  
  37.     };  
  38.     return (function(C) {  
  39.         if (typeof C == "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064"  C === null)  
  40.             return "null";  
  41.         else if (Object.prototype.toString.call(C) === "\u005b\u006f\u0062\u006a\u0065\u0063\u0074\u0020\u0041\u0072\u0072\u0061\u0079\u005d") {  
  42.             var B = ["\u005b"], G, E, D = C.length, F;  
  43.             for (E = 0; E < D; E += 1) {  
  44.                 F = C[E];  
  45.                 switch (typeof F) {  
  46.                     case "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064" :  
  47.                     case "\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e" :  
  48.                     case "\u0075\u006e\u006b\u006e\u006f\u0077\u006e" :  
  49.                         break;  
  50.                     default :  
  51.                         if (G)  
  52.                             B.push("\u002c");  
  53.                         B.push(F === null ? "null" : this.encode(F));  
  54.                         G = true 
  55.                 }  
  56.             }  
  57.             B.push("\u005d");  
  58.             return B.join("")  
  59.         } else if ((Object.prototype.toString.call(C) === "\u005b\u006f\u0062\u006a\u0065\u0063\u0074\u0020\u0044\u0061\u0074\u0065\u005d"))  
  60.             return "\"" + C.getFullYear() + "-" + _(C.getMonth() + 1) + "-" + _(C.getDate()) + "T" + _(C.getHours()) + ":" + _(C.getMinutes()) + ":" + _(C.getSeconds()) + "\"";  
  61.         else if (typeof C == "\u0073\u0074\u0072\u0069\u006e\u0067") {  
  62.             if (/["\\\x00-\x1f]/.test(C))  
  63.                 return "\"" + C.replace(/([\x00-\x1f\\"])/g, function(B, _) {  
  64.                     var $ = A[_];  
  65.                     if ($)  
  66.                         return $;  
  67.                     $ = _.charCodeAt();  
  68.                     return "\\u00" + Math.floor($ / 16).toString(16) + ($ % 16).toString(16)  
  69.                 }) + "\"";  
  70.             return "\"" + C + "\"" 
  71.         } else if (typeof C == "\u006e\u0075\u006d\u0062\u0065\u0072")  
  72.             return isFinite(C) ? String(C) : "null";  
  73.         else if (typeof C == "\u0062\u006f\u006f\u006c\u0065\u0061\u006e")  
  74.             return String(C);  
  75.         else {  
  76.             B = ["\u007b"], G, E, F;  
  77.             for (E in C)  
  78.                 if (!$  C.hasOwnProperty(E)) {  
  79.                     F = C[E];  
  80.                     if (F === null)  
  81.                         continue;  
  82.                     switch (typeof F) {  
  83.                         case "\u0075\u006e\u0064\u0065\u0066\u0069\u006e\u0065\u0064" :  
  84.                         case "\u0066\u0075\u006e\u0063\u0074\u0069\u006f\u006e" :  
  85.                         case "\u0075\u006e\u006b\u006e\u006f\u0077\u006e" :  
  86.                             break;  
  87.                         default :  
  88.                             if (G)  
  89.                                 B.push("\u002c");  
  90.                             B.push(this.encode(E), "\u003a", this.encode(F));  
  91.                             G = true 
  92.                     }  
  93.                 }  
  94.             B.push("\u007d");  
  95.             return B.join("")  
  96.         }  
  97.     })  
  98. })();  
  99. window.JSONUtil = JSONUtil; 

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索json
, 对象
, return
, function
, js+json
, 序列
js object对象
js对象序列化json、jquery序列化json对象、c对象序列化json、fastjson 序列化对象、java对象序列化成json,以便于您获取更多的相关知识。

时间: 2024-08-02 19:24:14

JS对象序列化为JSON对象的相关文章

jQuery实现form表单元素序列化为json对象的方法_jquery

本文实例讲述了jQuery实现form表单元素序列化为json对象的方法.分享给大家供大家参考,具体如下: 这段代码序列化form表单元素为json对象: <!Doctype html> <html xmlns=http://www.w3.org/1999/xhtml> <head> <title>jQuery扩展--form序列化到json对象</title> <meta http-equiv=Content-Type content=&

关于DateTime对象序列化为Json之后的若干问题

将Datetime对象序列化成Json对象是常有的事情,微软的序列化方法会将Datetime对象序列化成一个字符串: "\/Date(1234656000000)\/" 这样的字符串相当之不友好,但微软貌似喜欢,并且Json.net在4.5之前也是这么做的.现在的Json.net在序列化的时候,会将其转换为ISO标准时间: "2009-02-15T00:00:00Z" 在使用Json.net的时候,我们还可以使用其默认的DateTime转换器来处理,这样转换的字符串

将json对象转化为java对象?

问题描述 TestBean tb = new TestBean();tb.setName("闫燕飞");tb.setSex("男");List<String> list = new ArrayList<String>();list.add("mother");list.add("father");tb.setParentName(list);//将一个实体类对象转化为json对象JSONObject j

将JavaScript的jQuery库中表单转化为JSON对象的方法_jquery

大家知道Jquery中有serialize方法,可以将表单序列化为一个"&"连接的字符串,但却没有提供序列化为Json的方法.不过,我们可以写一个插件实现.   我在网上看到有人用替换的方法,先用serialize序列化后,将&替换成":"."'": /** * 重置form表单 * @param formId form的id */ function resetQuery(formId){ var fid = "#&qu

php 把对象转化为json

php中如何把对象转化为json呢? (1)对于普通的php对象 Php代码    //convert object to array   function object_to_array($obj){       if(is_array($obj)){           return $obj;       }       $_arr = is_object($obj)? get_object_vars($obj) :$obj;       foreach ($_arr as $key =>

JS判断是否为JSON对象及是否存在某字段的方法(推荐)_javascript技巧

实例如下: $.ajax({ type: 'POST', url: url, success(function(data){ //判断是否为JSON对象 if(typeof(data) == "object" && Object.prototype.toString.call(data).toLowerCase() == "[object object]" && !data.length){ alert("is JSON 0

JS遍历类、json对象属性、值,方便统一赋值

JS遍历类.json对象属性.值, 我们在页面,经常会接收后面或json返回的数据,要一个一个赋值,遍历json属性,可以方便给界面的控件赋值. 代码: 遍历js类 <script type="text/javascript"> //定义一个普通的js类,包含方法 var p= function (){ this.Id= 1; this.Name= 'test1'; } var pp= new p(); for(var item in pp){ if(typeof(pp[i

JS字符串转成JSON对象的几种方法

实例一, 在JS中,把 json 格式的字符串转成JSON对象,关键代码  代码如下 复制代码 json = eval('('+str+')'); 方法如下:  代码如下 复制代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=&

springmvc怎么接收json对象 springmvc接收json对象的方式

springmvc接收json对象的方式介绍: 1.以实体类方式接收 前端 ajax 提交数据: function fAddObj() { var obj = {}; obj['objname'] = "obj"; obj['pid'] = 1 ; $.ajax({ url: 'admin/Obj/addObj.do', method: 'post', contentType: 'application/json', // 这句不加出现415错误:Unsupported Media T