javascript对象封装后转换为json
来源:互联网 发布:淘宝店铺的运营管理 编辑:程序博客网 时间:2024/04/28 17:13
之前写了一个在Object原型加上一个转换json的工具,有一些bug和瑕疵在里面,而且改变Object的原型不是一个好的做法。
正好项目中有要用到,所以就重新写了一个我们项目所需要的结构来进行转换。
下载地址:http://download.csdn.net/download/w172087242/9600079
建议使用Google浏览器进行测试,插件支持所有主流浏览器
首先是ywkj-util.js
/** * Created by littlehow on 2016/8/9 0009. * @author littlehow */var YWKJ = {};/** util命名空间 */YWKJ.util = {};/** 字符串连接 */YWKJ.util.StringBuffer = function(){ this._value = []; this.add = this.append = function(s) { this._value.push(s); return this; } this.removeAdd = this.removeAppend = function() { this._value.pop(); return this; } this.valueOf = this.toString = function(split) { return this._value.join(split || ""); } this.getAddCnt = this.getAppendCnt = function(){ return this._value.length; } this.empty = this.clear = function(){ this._value = []; return this; } this.length = function(){ return this.toString().length; }}/** 获取对象toString */YWKJ.util.getString = function(obj) { if (obj === undefined || obj === null) return null; if(typeof obj == "string"){ return '"' + obj + '"'; } else if(typeof obj == "date"){ return obj.getTime(); } else { return obj; }}/** 获取对象的jsonString */YWKJ.util.forJsonString = function(obj) { if(obj === undefined || obj === null) return null; if(typeof obj == "function") return "{}"; var sb = new this.StringBuffer(); if(obj.constructor == Array) {//处理数组 if(obj.length == 0) return "[]"; sb.add("["); for(var i = 0, len = obj.length; i < len; i++){ var v = obj[i]; if(typeof v == "object") v = this.forJsonString(v); else v = this.getString(v); sb.add(v).add(", "); } if(sb.getAddCnt() > 1) sb.removeAdd(); return sb.add("]").toString(); }else if(typeof obj == "object"){ sb.add("{"); for(var i in obj){ var v = obj[i]; if(v === undefined || v === null) continue; var type = typeof v; if(type == "function"){ continue; } else{ if(typeof v == "object") v = this.forJsonString(v); else v = this.getString(v); sb.add('"').add(i).add('" : ').add(v).add(", "); } } if(sb.getAddCnt() > 1) sb.removeAdd(); return sb.add("}").toString(); } else { return null; }}
然后是实体类空间ywkj-pojo.js
/** * pojo.js file * @author littlehow * @time 2016-08-09 10:17 * @dependence ywkj-util.js *//** pojo命名空间 */YWKJ.pojo = {};YWKJ.pojo.ParentFunctionBean = function(){ this.toJson = function() { return YWKJ.util.forJsonString(this); }}
最后是bean空间ywkj-pojo-report.js
/** * @author littlehow * @time 2016-08-09 11:55 * @dependence ywkj-pojo.js *//** package report */YWKJ.pojo.report = { p : YWKJ.pojo};/** 浅继承 */YWKJ.pojo.report.extendFunc = function(son, parent) { for(var i in parent) { if(!son.prototype[i]) {//不存在则可以继承 son.prototype[i] = parent[i]; } }}YWKJ.pojo.report.extendsAll = function() { var parent = new this.p.ParentFunctionBean(); this.extendFunc(this.ReportBase, parent); this.extendFunc(this.Column, parent); this.extendFunc(this.Param, parent); this.extendFunc(this.ReportBean, parent); this.extendFunc(this.Template, parent); this.extendFunc(this.Sql, parent);}/** * 方法继承bean * @constructor */YWKJ.pojo.report.ParentFunctionBean = function(){ this.toJson = function() { return YWKJ.util.forJsonString(this); }}/** * 报表基础信息 * @param reportName * @param userId * @param userName * @param useFlag * @param platform * @constructor */YWKJ.pojo.report.ReportBase = function(reportName, userId, userName, useFlag, platform) { this.reportName = reportName; this.createUserid = userId; this.createUser = userName; this.useState = useFlag; this.updateUserid = null; this.updateUser = null; this.id = null; this.platform = platform;}/** * 参数信息 * @param name * @param desc * @param code * @param type * @param map * @param _default * @constructor */YWKJ.pojo.report.Param = function(name, desc, code, type, map, _default){ this.paramName = name; this.paramDesc = desc; this.paramCode = code; this.paramType = type; this.paramMap = map; this.paramDefault = _default; this.operType = null; this.id = null;}/** * 展示字段信息 * @param code * @param name * @param note * @constructor */YWKJ.pojo.report.Column = function(code, name, note){ this.columnCode = code; this.columnName = name; this.note = note; this.operType = null;}/** * 语句信息 * @param sql * @param id * @constructor */YWKJ.pojo.report.Sql = function(sql, id) { this.sql = sql; this.id = id;}/** * 模板信息pojo * @param content * @param sql * @param params * @param columns * @constructor */YWKJ.pojo.report.Template = function(name, type, code, content, sql, soutNum, params, columns) { this.templateName = name; this.templateType = type; this.templateCode = code; this.content = content; this.sqlInfo = sql; this.soutNum = soutNum; this.params = params; this.columns = columns; this.operType = null; this.id = null;}/** * 报表信息 * @param base * @param templates * @constructor */YWKJ.pojo.report.ReportBean = function(base, templates) { this.base = base; this.templates = templates;}/** * 执行继承 */YWKJ.pojo.report.extendsAll();
调用html代码:
<!doctype html><html lang="en"> <head> <meta charset="UTF-8"> <title>report json test</title> <script type="text/javascript" src="../js/ywkj-util.js"></script> <script type="text/javascript" src="../js/ywkj-pojo.js"></script> <script type="text/javascript" src="../js/ywkj-pojo-report.js"></script> </head> <body><div id="mydiv" style="width:600px;background-color:black;color:orange;"></div> </body> <script type="text/javascript"> var $ = YWKJ.pojo.report; var columns = [new $.Column("userid","用户id","登录者"), new $.Column("userid","用户id","登录者")]; columns[0].operType = "2"; columns[1].operType = "1"; var params = [new $.Param("用户id", "用户id", "string1", "string", "userid", "171")]; params[0].operType = "3"; params[0].id = 777; var templates = [new $.Template("用户增长曲线图", "散点图", "x001", "这是是示例模板, 欢迎光临 ${userid}", new $.Sql("select * from user_info where userid=#{string1}", 133), 0, params, columns)]; templates[0].operType = "2"; templates[0].id = 168; var base = new $.ReportBase("用户浏览报表", null, null, false, "终端宝"); base.updateUserid = "18561561"; base.updateUser = "陈浩南"; var report = new $.ReportBean(base, templates); console.log(report.toJson()); console.log(base.toJson()); console.log(templates[0].toJson()); document.getElementById("mydiv").innerHTML = report.toJson(); console.log((new $.ReportBean()).toJson()); </script></html>
0 0
- javascript对象封装后转换为json
- javascript 转换Json字符串为JSON对象
- JSON 字符串转换为 JavaScript 对象
- 把 JSON 文本转换为 JavaScript 对象
- JavaScript将对象转换为JSON
- 把 JSON 文本转换为 JavaScript 对象
- 封装将json对象转换为java集合对象
- javaScript的JSON字符串转换为JSON对象。
- javascript将json字符串转换为json对象
- JavaScript 对象表示法 (JSON) 字符串转换为对象
- json转换为对象
- 转换json格式的日期为Javascript对象
- javascript 将对象数组转换为json字符串
- javaScript对象(object)转换为JSON格式
- javascript 数组的使用 + js 对象 转换为json 字符串
- 如何将json文本转换为JavaScript对象
- 使用eval函数把 JSON 文本转换为 JavaScript 对象
- JSON.stringify()方法将javascript对象转换为字符串
- html的header结构和实例
- CLIR CLIP
- Python黑帽编程2.6 模块
- 【Python】|Python中一个map()小应用
- vxworks开发基础
- javascript对象封装后转换为json
- android jni 编程
- java高级开发学习之--反射机制(1)
- 百度编辑器UEditor 图片上传到资源服务器 JSP
- hdu5812Distance(数学)
- 基于opencv3.1的cnn分类系统集成
- 各个字号对应的px大小
- how to import github Oauth token
- Trie/Xor