1
我有以下格式的表格(只有表单的部分)构建JSON对象从形式
<input type="text" name="Journal[data][serviceaddress][company]" value="{{company}}"/>
<input type="text" name="Journal[data][serviceaddress][address]" value="{{address}}"/>
<input type="text" name="Journal[data][serviceaddress][zip]" value="{{zip}}"/>
<textarea name="Journal[data][serviceaddress][notes]">{{notes}}</textarea>
<input type="text" name="Journal[rows][1][title]"/>
<input type="text" name="Journal[rows][1][body]"/>
<input type="text" name="Journal[rows][2][title]"/>
<input type="text" name="Journal[rows][2][body]"/>
我想将其转换为以下JSON对象
{
data:{
serviceaddress:{
company: "companyvalue",
address: "addressvalue",
zip: "zipvalue",
notes: "notesvalue"
}
},
rows:{
1:{
title: "row1title",
body: "row1body"
},
2:{
title: "row2title",
body: "row2body"
}
}
}
这将是最好的方式来做到这一点?我想,我一定是人谁已经这样做,但我发现在搜索时是人想输入的名称为重点,而不是嵌套的JSON数据...
你想这样做在客户端或服务器端? – Ares 2013-03-22 06:41:37
假设你想这样做在JQuery中,检出连载API - http://api.jquery.com/serialize/。这可能会帮助你。 – ryadavilli 2013-03-22 06:42:23
jQuery有form.serializeArray()来... – karaxuna 2013-03-22 06:44:02