如何将序列化的表单转换为具有标签列表的json格式
How to convert serialized form to json format which having a list of tags
这是我的表单:
<form>
<input type="text" value="value1a" name="parentobject.childobject[0].field1"/>
<input type="text" value="value2a" name="parentobject.childobject[0].field2"/>
<input type="text" value="value3a" name="parentobject.childobject[0].field3"/>
<input type="text" value="value1b" name="parentobject.childobject[1].field1"/>
<input type="text" value="value2b" name="parentobject.childobject[1].field2"/>
<input type="text" value="value3b" name="parentobject.childobject[1].field3"/>
</form>
当我使用JSON.stringfy($('form').serializeArray())时,值转换为JSON,但不是我预期的结果。
使用JSON.stringfy($('form).serializedArray())后的输出
{
"parentobject.childobject[0].field1" : "value1a"
"parentobject.childobject[0].field2" : "value2a"
"parentobject.childobject[0].field3" : "value3a"
"parentobject.childobject[1].field1" : "value1b"
"parentobject.childobject[1].field2" : "value2b"
"parentobject.childobject[1].field3" : "value3b"
}
这是我预期的结果:
{
"parentobject": {
"childobject": [
{
"field1": "value1a",
"field2": "value2a",
"field3": "value3a"
},
{
"field1": "value1b",
"field2": "value2b",
"field3": "value3b"
}
]
}
}
我希望有人能帮忙。提前谢谢。
您可以使用eval()
功能
var parentobject = {
childobject: [{}, {}]
}
var arr = $('form').serializeArray();
for(var i=0;i<arr.length;i++)
{
eval(arr[i].name+"='"+arr[i].value+"'");
}
alert(JSON.stringify(parentobject))
演示
请参阅http://jsfiddle.net/vorj5o1b/1/
serializeArray()
只获取单个对象中字段的name
和value
属性。如果你想自定义你得到的输出,你需要编写自己的函数,这样你就可以创建自己的自定义数组,并在自定义数组上调用stringify
。
var myArray = [];
var fields = $("#f").serializeArray();
var obj = {};
var childObj = {};
var firstIndex = 0;
jQuery.each(fields, function(i, field){
var parentName = field.name.split('.')[0];
var childName = field.name.split('.')[1].split("[")[0];
var childIndex = parseInt(field.name.split('.')[1].split("[")[1].split("]")[0]);
var fieldName = field.name.split('.')[2];
if (!obj[parentName]){ //If obj doesn't have the parentName object, create it.
obj[parentName] = {};
}
if (!obj[parentName][childName]){ //if obj[parentName] doesn't have an array yet, create it
obj[parentName][childName] = [];
}
if (firstIndex !== childIndex){ //when it moves from [0] to [1]
firstIndex = childIndex;
obj[parentName][childName].push(childObj); //push childObj to obj array
childObj = {};
}
childObj[fieldName] = field.value;
if (i == fields.length - 1){ //needs an extra if here to add the final childObj
obj[parentName][childName].push(childObj);
}
});
myArray.push(obj); //use custom array
var myString = JSON.stringify(myArray); //stringify custom array
console.log(myString);
serializeArray
方法不会解析字段名来派生层次结构;你必须手动编码,但这并不太难。如果您有一个定义良好的格式,您可以尝试下面的代码。如果您需要一个更通用的解决方案,允许多个级别的嵌套和不同的父名称,那么您需要构建功能更全面的解决方案。
$(function() {
var $form = $("form");
var regex = /.*childobject'[('d)'].*/;
var rgxForField = /.*childobject.*(field'd)/;
var obj = {"parentobject" : {} };
var childObjects = [];
obj["parentobject"]["childobject"] = childObjects;
$form.find("input[name^='parentobject.childobj']").each(function() {
var idx = parseInt(this.name.match(regex)[1], 10);
if (!childObjects[idx]) childObjects[idx] = {};
var fieldName = this.name.match(rgxForField)[1];
childObjects[idx][fieldName] = this.value;
});
console.log(obj);
$form.after($("<div/>", { text: JSON.stringify(obj)}));
$form.remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form>
<input type="text" value="value1a" name="parentobject.childobject[0].field1"/>
<input type="text" value="value2a" name="parentobject.childobject[0].field2"/>
<input type="text" value="value3a" name="parentobject.childobject[0].field3"/>
<input type="text" value="value1b" name="parentobject.childobject[1].field1"/>
<input type="text" value="value2b" name="parentobject.childobject[1].field2"/>
<input type="text" value="value3b" name="parentobject.childobject[1].field3"/>
</form>
相关文章:
- 对象 Javascript 中的标签无效 - 想要添加事件列表器
- 如何为给定对象附加标签列表
- jQuery Tag-It – 如何在选择标签时自动显示自动完成列表
- 在jquery javascript中单击下拉列表时更改标签
- 如何使用 jquery 检查该值是否存在于带有标签的 select2 下拉列表中:true
- asp.net 数据列表中的跟踪标签 ID
- 将列表拆分为两列.列表没有't具有块,但是需要在最接近的<p>标签
- 在CKEditor 4.x中,有没有办法在初始化后获取允许的标签列表
- 如何使用 js 动态创建 标签列表
- 如何使用引导验证程序验证输入标签列表
- 如何将序列化的表单转换为具有标签列表的json格式
- 匹配推文中可能的标签列表-Javascript或jQuery
- 活动类默认为标签列表
- 标签列表过滤动画
- 如何从标签列表中获取对象属性到javascript变量
- Set class='selected '在多个选项标签(列表/菜单)中
- 按母语人士的数量寻找一个好的ISO语言标签列表
- 使用Flickr API获取指定照片集的标签列表
- 使用Javascript Use API获取标签列表
- Javascript库/ jquery插件,使我能够单击标签列表输入它们