search
HomeWeb Front-endJS TutorialAn extended form based on jQuery serialized to json object_jquery

Copy code The code is as follows:

$.fn.serializeObject = function() {
var o = {};
var a = this.serializeArray();
$.each(a, function() {
if (o[this.name]) {
if (!o[ this.name].push) {
o[this.name] = [ o[this.name] ];
}
o[this.name].push(this.value || '' );
} else {
o[this.name] = this.value || '';
}
});
return o;
}

This function is sufficient for ordinary object conversion, but it cannot be supported if the object contains sub-objects.
For example, I have a form like this:
Copy code The code is as follows:







The domain class corresponding to the server is as follows:
Copy code The code is as follows:

public class DummyProduct {

private DummyCategory category;
private String name;

public DummyCategory getCategory() {
return category;
}
public void setCategory(DummyCategory category) {
this.category = category;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

}
public class DummyCategory {
private String id;
private String name;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

If you want to serialize form data into a json string that matches the server-side domain class, you can use my following extension
Copy code The code is as follows:

/**@serializedParams looks like "prop1=value1&prop2=value2".  
Nested property like 'prop.subprop=value' is also supported **/
function paramString2obj (serializedParams) {

var obj={};
function evalThem (str) {
var attributeName = str.split("=")[0];
var attributeValue = str.split("=")[1];
if(!attributeValue){
return ;
}

var array = attributeName.split(".");
for (var i = 1; i var tmpArray = Array();
tmpArray.push("obj");
for (var j = 0; j tmpArray.push(array[j]);
};
var evalString = tmpArray.join(".");
// alert(evalString);
if(!eval(evalString)){
eval(evalString "={ };");
}
};
eval("obj." attributeName "='" attributeValue "';");

};
var properties = serializedParams .split("&");
for (var i = 0; i evalThem(properties[i]);
};
return obj;
}
$.fn.form2json = function(){
var serializedParams = this.serialize();
var obj = paramString2obj(serializedParams);
return JSON.stringify(obj) ;
}

It probably looks like this:
Copy code The code is as follows:

var json = $("#testform").form2json();
alert(json);
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
在Java中,我们如何使用flexjson序列化对象列表?在Java中,我们如何使用flexjson序列化对象列表?Sep 05, 2023 pm 11:09 PM

Flexjson是一个轻量级库,用于序列化和反序列化Java对象>和来自JSON格式。我们可以使用JSONSerializer类的serialize()方法序列化对象列表。此方法可以对目标实例执行浅层序列化。我们需要将列表类型的对象列表作为参数传递给serialize()方法。语法publicStringserialize(Objecttarget)示例importflexjson.JSONSerializer;importjava.util.*;publicclassJsonSerial

Java序列化如何影响性能?Java序列化如何影响性能?Apr 16, 2024 pm 06:36 PM

序列化对Java性能的影响:序列化过程依赖于反射,会显著影响性能。序列化需要创建字节流存储对象数据,导致内存分配和处理成本。序列化大对象会消耗大量内存和时间。序列化后的对象在网络上传输时会增加负载量。

C++ 函数库如何进行序列化和反序列化?C++ 函数库如何进行序列化和反序列化?Apr 18, 2024 am 10:06 AM

C++函数库序列化和反序列化指南序列化:创建输出流并将其转换为存档格式。将对象序列化到存档中。反序列化:创建输入流并将其从存档格式恢复。从存档中反序列化对象。实战示例:序列化:创建输出流。创建存档对象。创建对象并将其序列化到存档中。反序列化:创建输入流。创建存档对象。创建对象并从存档中反序列化。

如何使用Java中的Jackson库对属性的顺序进行序列化?如何使用Java中的Jackson库对属性的顺序进行序列化?Aug 28, 2023 pm 12:45 PM

@JsonPropertyOrder是在类级别使用的注释。它采用字段列表作为属性,该列表定义字段在对象JSON序列化生成的字符串中出现的顺序。可以首先序列化注释声明中包含的属性(按定义的顺序),然​​后序列化定义中未包含的任何属性。语法public@interfaceJsonPropertyOrder示例importcom.fasterxml.jackson.core.*;importcom.fasterxml.jackson.databind.*;importcom.fasterxml.jac

PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化Jul 29, 2023 am 10:49 AM

PHP数据处理技巧:如何使用serialize和unserialize函数实现数据序列化与反序列化序列化和反序列化是在计算机科学中常用的数据处理技巧之一。在PHP中,我们可以使用serialize()和unserialize()函数来实现数据的序列化和反序列化操作。本文将为您详细介绍如何使用这两个函数,并提供相关代码示例。一、什么是序列化和反序列化在计算机编

如何使用Java中的flexjson库序列化一个map?如何使用Java中的flexjson库序列化一个map?Aug 26, 2023 pm 08:13 PM

Flexjson是一个轻量级库,用于将Java对象序列化为JSON格式以及反序列化为JSON格式。我们还可以使用JSONSerializer类的serialize()方法来序列化Map,它对目标实例执行浅层序列化。语法publicStringserialize(Objecttarget)示例importflexjson.JSONSerializer;importjava.util.*;publicclassJsonSerializeMapTest{  publ

在Java中,序列化和反序列化有什么区别?在Java中,序列化和反序列化有什么区别?Apr 16, 2024 am 08:54 AM

序列化将对象转换为字节序列,反序列化将字节序列还原为对象。序列化用于持久化或传输对象,而反序列化用于重建对象。实战案例中,用户对象序列化写入文件,然后反序列化读出,演示了序列化和反序列化在Java中的实际应用。

PHP数组的序列化和反序列化方法和注意事项PHP数组的序列化和反序列化方法和注意事项Jul 16, 2023 pm 06:37 PM

PHP数组的序列化和反序列化方法和注意事项在PHP中,数组是一种非常常见和重要的数据类型。当我们需要在不同的程序之间或在不同的请求之间传递数组时,就需要将数组进行序列化和反序列化。本文将介绍PHP中数组的序列化和反序列化方法以及相关的注意事项。序列化数组在PHP中,可以使用serialize()函数将一个数组序列化为一个字符串。该函数的用法如下所示:$arr

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment