Including the collection attribute in the object and the reference of other object attributes in the object:
CopyCode The Code is as follows :/**
** Set JSON object data to form fields
*/
Function jsonobjecttoform (Form, jsonobject ){
For (I = 0, max = form. elements. length; I <Max; I ++ ){
E = form. elements [I];
Ename = E. Name;
If (ename. indexof ('.')> 0 ){
Dotindex = ename. indexof ('.');
Parentname = ename. substring (0, dotindex );
Childname = ename. substring (dotindex + 1 );
// Iteratively determine the ename and assemble it into a JSON Data Structure
Evalue = itervaluefromjsonobject (jsonobject, parentname, childname );
} Else {
Evalue = jsonobject [ename];
}
If (evalue & evalue! = "Undefined" & evalue! = "Null "){
Switch (E. Type ){
Case 'checkbox ':
Case 'Radio ':
If (E. value = evalue ){
E. Checked = true;
}
Break;
Case 'ddden ':
Case 'Password ':
Case 'textarea ':
Case 'text ':
E. value = evalue;
Break;
Case 'select-one ':
Case 'select-multiple ':
For (j = 0; j <E. Options. length; j ++ ){
OP = E. Options [J];
// Alert ("ename:" + ename + "; OP value:" + OP. Value + "; evalue:" + evalue );
If (op. value = evalue ){
Op. Selected = true;
}
}
Break;
Case 'click ':
Case 'file ':
Case 'image ':
Case 'reset ':
Case 'submit ':
Default:
}
}
}
}
/**
* Two methods are available for reading and writing JSON arrays.
* 1: A. BS [0]. ID
* 2: A ["BS"] [0] ["ID"]
* Convert a form to a JSON Data Format
*/
Function formtojsonobject (form ){
VaR jsonobject = {};
For (I = 0, max = form. elements. length; I <Max; I ++ ){
E = form. elements [I];
Em = new array ();
If (E. type = 'select-multiple '){
For (j = 0; j <E. Options. length; j ++ ){
OP = E. Options [J];
If (op. Selected ){
Em [em. Length] = op. value;
}
}
}
Switch (E. Type ){
Case 'checkbox ':
Case 'Radio ':
If (! E. Checked) {break ;}
Case 'ddden ':
Case 'Password ':
Case 'select-one ':
Case 'select-multiple ':
Case 'textarea ':
Case 'text ':
Ename = E. Name;
If (E. type = 'select-multiple '){
Evalue = em;
} Else {
Evalue = E. value. Replace (New Regexp ('(["\\\\])', 'G'), '\\$ 1 ');
}
// Determine whether the data is of the Object Type
If (ename. indexof ('.')> 0 ){
Dotindex = ename. indexof ('.');
Parentname = ename. substring (0, dotindex );
Childname = ename. substring (dotindex + 1 );
// Iteratively determine the ename and assemble it into a JSON Data Structure
Iterjsonobject (jsonobject, parentname, childname, evalue );
} Else {
Jsonobject [ename] = evalue;
}
Break;
Case 'click ':
Case 'file ':
Case 'image ':
Case 'reset ':
Case 'submit ':
Default:
}
}
Return jsonobject;
}
/**
* Iteratively convert form elements into JSON data
*/
Function iterjsonobject (jsonobject, parentname, childname, evalue ){
// Parrayindex is used to determine whether an element is an array identifier.
Parrayindex = parentname. indexof ('[');
// Determine whether to set data, not just Object Attributes
If (parrayindex <0 ){
VaR child = jsonobject [parentname];
If (! Child ){
Jsonobject [parentname] = {};
}
Dotindex = childname. indexof ('.');
If (dotindex> 0 ){
Iterjsonobject (jsonobject [parentname], childname. substring (0, dotindex), childname. substring (dotindex + 1), evalue );
} Else {
Jsonobject [parentname] [childname] = evalue;
}
} Else {
Parray = jsonobject [parentname. substring (0, parrayindex)];
// If the JS array does not exist, an array type is initialized.
If (! Parray ){
Jsonobject [parentname. substring (0, parrayindex)] = new array ();
}
// Obtain the set subscript and determine whether the corresponding subscript has a JS object
Arrayindex = parentname. substring (parrayindex + 1, parentname. Length-1 );
VaR c = jsonobject [parentname. substring (0, parrayindex)] [arrayindex];
If (! C ){
Jsonobject [parentname. substring (0, parrayindex)] [arrayindex] = {};
}
Dotindex = childname. indexof ('.');
If (dotindex> 0 ){
Iterjsonobject (jsonobject [parentname. substring (0, parrayindex)] [arrayindex], childname. substring (0, dotindex), childname. substring (dotindex + 1), evalue );
} Else {
Jsonobject [parentname. substring (0, parrayindex)] [arrayindex] [childname] = evalue;
}
}
}
/**
* Set iterative JSON data objects to form fields
*/
Function itervaluefromjsonobject (jsonobject, parentname, childname ){
// Parrayindex is used to determine whether an element is an array identifier.
Parrayindex = parentname. indexof ('[');
// Determine whether to set data, not just Object Attributes
If (parrayindex <0 ){
Dotindex = childname. indexof ('.');
If (dotindex> 0 ){
Return itervaluefromjsonobject (jsonobject [parentname], childname. substring (0, dotindex), childname. substring (dotindex + 1 ));
} Else {
Return jsonobject [parentname] [childname]
}
} Else {
Parray = jsonobject [parentname. substring (0, parrayindex)];
// Obtain the set subscript and determine whether the corresponding subscript has a JS object
Arrayindex = parentname. substring (parrayindex + 1, parentname. Length-1 );
VaR c = jsonobject [parentname. substring (0, parrayindex)] [arrayindex];
Dotindex = childname. indexof ('.');
If (dotindex> 0 ){
Return itervaluefromjsonobject (jsonobject [parentname. substring (0, parrayindex)] [arrayindex], childname. substring (0, dotindex), childname. substring (dotindex + 1 ));
} Else {
Return jsonobject [parentname. substring (0, parrayindex)] [arrayindex] [childname]
}
}
}