/**
* 将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;
};
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相同字段值
将字符串解析成Json对象
var str = '{"key1": "value1","key2": "value2","key3": "value3"}';
var obj = JSON.parse(str);
将Json对象解析成字符串
var str = JSON.stringify(obj);