jQuery serializeObject 序列化 form 表单 Json 对象 jQuery.extend() 合并对象

蔺霄
2023-12-01

1、jQuery serializeObject 序列化form表单Json对象

/**
 *  将form序列化Json对象
 * {key1:"value1",key2:"value2"}
 * @example
 * <script>
 * var formParams = $("#formId").serializeObject();
 * </script>
 */
$.prototype.serializeObject = function() {
	var a, o, h, i, e;
	a = this.serializeArray();
	o = {};
	h = o.hasOwnProperty;
	for (i = 0; i < a.length; i++) {
		e = a[i];
		if (!h.call(o, e.name)) {
			o[e.name] = e.value;
		}
	}
	return o;
};


2、jQuery.extend() 合并对象

var jsonData = {
					key1: "value1",
					key2: "value2",
					key3: "value3"
				};
				var formParams = $("#formId").serializeObject();
				var params = $.extend(true, jsonData, formParams);
//params为jsonData和formParams合并后的对象,如果jsonData和formParams有相同字段,则formParams覆盖jsonData相同字段值

3、JSON.parse()

将字符串解析成Json对象

	var str = '{"key1": "value1","key2": "value2","key3": "value3"}';
	var obj = JSON.parse(str);

4、JSON.stringify()

将Json对象解析成字符串

var str = JSON.stringify(obj);



 类似资料: