Page 1/3 _ json

Source: Internet
Author: User
I found several javascript frameworks and did not find what I wanted: Provide functions, encapsulate all the fields of a form into objects in json data format, and only implement one by myself. Including the collection attribute in the object and the reference of other object attributes in the object:

/***** 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 judge the eName and assemble it into a json Data Structure eValue = iterValueFromJsonObject (jsonObject, parentName, childName);} e Lse {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 'did': case 'Password': case 'textea ': 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 :}}}} /*** there are two ways to read and write json arrays * 1:. bs [0]. id * 2: a ["bs"] [0] ["id"] * converts 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. op Tions. 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 'texta': case 'text ': eName = e. name; if (e. type = 'select-multiple ') {eValue = em;} else {eValue = e. value. replace (new RegExp ('(["\\\\])', 'G'), '\\$ 1 ');} // determine whether it is an object type data if (eName. indexOf ('. ')> 0) {dotIndex = eName. indexOf ('. '); parentName = eName. substring (0, dotIndex); childName = eName. subst Ring (dotIndex + 1); // iteratively judge 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 ;} /*** convert form Element iteration to json data */function iterJsonObject (jsonObject, parentName, childName, eValue) {// pArrayIndex is used to determine whether the element is an array with pArrayIndex = parentName. inde XOf ('['); // determines whether data is set. 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 no js array exists, an array type if (! PArray) {jsonObject [parentName. substring (0, pArrayIndex)] = new Array ();} // obtain the set subscript and determine whether the corresponding subscript contains the 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; }}/ *** sets the iterative json data object to the form field */function iterValueFromJsonObject (jsonObject, parentName, childName) {// pArrayIndex is used to determine whether the element is an array with pArrayIndex = parentName. indexOf ('['); // determines whether data is set. 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 exists. 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]}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.