`
lan13217
  • 浏览: 502974 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

js Objec->String

 
阅读更多
function obj2string(o){
	var r=[];
	if(typeof o=="string"){
		return "\""+o.replace(/([\'\"\\])/g,"\\$1").replace(/(\n)/g,"\\n").replace(/(\r)/g,"\\r").replace(/(\t)/g,"\\t")+"\"";
	}
	if(typeof o=="object"){
		if(!o.sort){
			for(var i in o){
				r.push(i+":"+obj2string(o[i]));
			}
			if(!!document.all&&!/^\n?function\s*toString\(\)\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)){
				r.push("toString:"+o.toString.toString());
			}
			r="{"+r.join()+"}";
		}else{
			for(var i=0;i<o.length;i++){
				r.push(obj2string(o[i]))
			}
			r="["+r.join()+"]";
		} 
		return r;
	} 
	return o.toString();
}

function writeObj(obj){
	var description = "";
	for(var i in obj){  
		var property=obj[i];  
		description+=i+" = "+property+"\n"; 
	}  
	alert(description);
}



Extjs obj->json
myobj = Ext.decode("{a:\"a\",b:\"b\"}")

Extjs json->obj
myobj = Ext.encode(myobj)


Extjs decode
function (json){return eval("("+json+")")} 



Extjs encode
function (o) {
	if (typeof o == "undefined" || o === null) {
		return "null"
	} else {
		if (Ext.isArray(o)) {
			return encodeArray(o)
		} else {
			if (Ext.isDate(o)) {
				return encodeDate(o)
			} else {
				if (typeof o == "string") {
					return encodeString(o)
				} else {
					if (typeof o == "number") {
						return isFinite(o) ? String(o) : "null"
					} else {
						if (typeof o == "boolean") {
							return String(o)
						} else {
							var a = ["{"],
							b,
							i,
							v;
							for (i in o) {
								if (!useHasOwn || o.hasOwnProperty(i)) {
									v = o[i];
									switch (typeof v) {
									case "undefined":
									case "function":
									case "unknown":
										break;
									default:
										if (b) {
											a.push(",")
										}
										a.push(this.encode(i), ":", v === null ? "null" : this.encode(v));
										b = true
									}
								}
							}
							a.push("}");
							return a.join("")
						}
					}
				}
			}
		}
	}
}

分享到:
评论

相关推荐

    read-glob-promise:承诺使用 glob 模式读取文件

    阅读全局承诺 版本: 使用 glob 模式搜索文件并异步读取它们 var readGlob = require ( 'read-glob-promise' ) ;readGlob ( '*.txt' )....readGlob(模式[,选项]) 模式: String (全局模式) 选项: Objec

    multimatch:扩展minimatch.match()以支持多种模式

    多重比对 扩展以支持多种模式 安装 $ npm install multimatch 用法 const multimatch = require ( 'multimatch' ) ; multimatch ( [ 'unicorn' , 'cake' , 'rainbows' ] , [ '*' , '!...类型: objec

    浅谈Javascript中深复制

    在javascript中,所有的object变量之间的赋值都是传地址的,可能有同学会问哪些是object对象。举例子来说明可能会比较好: 代码如下: typeof(true) //”boolean” typeof(1) //”number” typeof(“1″) //”...

    astorage:用于localStorage的微型API包装器

    储存 用于localStorage微型API包装器,可让您安全地保存数字,数组,对象和其他数据类型。 安装 可通过npm或bower获得Astorage。 npm install astorage ...// value — string, number, array, objec

    python3.6.5参考手册 chm

    The json module: JavaScript Object Notation The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific ...

    关于微信上网页图片点击全屏放大效果

    这个代码在网上搜一下,... if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\""; if (typeof o == "objec

Global site tag (gtag.js) - Google Analytics