您好,欢迎访问一九零五行业门户网

javascript表单域与json数据间的交互第1/3页_json

包括对象中有集合属性、对象中引用其他对象属性:
/** **json对象数据设置到表单域中 */ 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); //迭代判断ename,组装成json数据结构 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 'hidden': 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 'button': case 'file': case 'image': case 'reset': case 'submit': default: } } } } /** * json数组读写有两种方式 * 1: a.bs[0].id * 2: a["bs"][0]["id"] * 把表单转换成json数据格式 */ 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 'hidden': 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'); } //判断是否是对象类型数据 if(ename.indexof('.') > 0){ dotindex = ename.indexof('.'); parentname = ename.substring(0, dotindex); childname = ename.substring(dotindex+1); //迭代判断ename,组装成json数据结构 iterjsonobject(jsonobject, parentname, childname, evalue); }else{ jsonobject[ename] = evalue; } break; case 'button': case 'file': case 'image': case 'reset': case 'submit': default: } } return jsonobject; } /** * 把表单元素迭代转换成json数据 */ function iterjsonobject(jsonobject, parentname, childname, evalue){ //parrayindex用于判断元素是否是数组标示 parrayindex = parentname.indexof('['); //判断是否集合数据,不是则只是对象属性 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)]; //若不存在js数组,则初始化一个数组类型 if(!parray){ jsonobject[parentname.substring(0, parrayindex)] = new array(); } //取得集合下标,并判断对应下标是否存在js对象 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; } } } /** * 迭代json数据对象设置到表单域中 */ function itervaluefromjsonobject(jsonobject, parentname, childname){ //parrayindex用于判断元素是否是数组标示 parrayindex = parentname.indexof('['); //判断是否集合数据,不是则只是对象属性 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)]; //取得集合下标,并判断对应下标是否存在js对象 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] } } }
其它类似信息

推荐信息