html - 使用 HTML 表单提交对象的 JSON 数组

标签 html css json

我有以下 HTML,它以下面的格式提交 JSON:

HTML:

<label for="renewal_date">Renewal Date</label><input type="text" name="custom_form_fields[renewal_date[value]" id="renewal_date" class="form-control">
<input type="hidden" name="custom_form_fields[renewal_date[columns]]" value="4">
<input type="hidden" name="custom_form_fields[renewal_date[content]]" value="">
<input type="hidden" name="custom_form_fields[renewal_date[new_row]]" value="false">
<input type="hidden" name="custom_form_fields[renewal_date[field_type]]" value="text">


<label for="hold_up">Hold Up</label><input type="checkbox" name="custom_form_fields[hold_up[value]" id="hold_up">
<input type="hidden" name="custom_form_fields[hold_up[columns]]" value="3">
<input type="hidden" name="custom_form_fields[hold_up[content]]" value="">
<input type="hidden" name="custom_form_fields[hold_up[new_row]]" value="false">
<input type="hidden" name="custom_form_fields[hold_up[field_type]]" value="check box">


<label for="to">To</label><input type="text" name="custom_form_fields[to[value]" id="to" class="form-control">
<input type="hidden" name="custom_form_fields[to[columns]]" value="12">
<input type="hidden" name="custom_form_fields[to[content]]" value="">
<input type="hidden" name="custom_form_fields[to[new_row]]" value="false">
<input type="hidden" name="custom_form_fields[to[field_type]]" value="text">

<label for="attention">Attention</label><input type="text" name="custom_form_fields[attention[value]" id="attention" class="form-control">
<input type="hidden" name="custom_form_fields[attention[columns]]" value="12">
<input type="hidden" name="custom_form_fields[attention[content]]" value="">
<input type="hidden" name="custom_form_fields[attention[new_row]]" value="false">
<input type="hidden" name="custom_form_fields[attention[field_type]]" value="text">

JSON:

"custom_form_fields"=>
{
    "renewal_date"=>{"value"=>"asdc", "columns"=>"4", "content"=>"","new_row"=>"false", "field_type"=>"text"},
    "hold_up"=>{"value"=>"on", "columns"=>"3", "content"=>"","new_row"=>"false", "field_type"=>"check box"},
    "to"=>{"value"=>"asdc", "columns"=>"12", "content"=>"", "new_row"=>"false", "field_type"=>"text"},
    "attention"=>{"value"=>"asdc", "columns"=>"12", "content"=>"", "new_row"=>"false", "field_type"=>"text"}
}

我希望 JSON 将所有自定义表单字段属性格式化为没有指针的对象,如下所示:

 "custom_form_fields"=>
{
    {"name" => "renewal_date", "value"=>"asdc", "columns"=>"4", "content"=>"", "new_row"=>"false", "field_type"=>"text"},
    {"name" => "hold_up",  "value"=>"on", "columns"=>"3", "content"=>"", "new_row"=>"false", "field_type"=>"check box"},
    {"name" => "to",  "value"=>"asdc", "columns"=>"12", "content"=>"", "new_row"=>"false", "field_type"=>"text"},
    {"name" => "attention",  "value"=>"asdc", "columns"=>"12", "content"=>"", "new_row"=>"false", "field_type"=>"text"}
}

我不确定如何格式化我的 HTML 以生成此 JSON。任何帮助,将不胜感激。谢谢!

最佳答案

您可以用正则表达式替换。我只是编写代码:

regexr.com/47uol

使用此正则表达式进行匹配:\n+[\s|\,]*(\"\w*")(\=\>)(\{)

并替换为: \n{"name"=> $1,

关于html - 使用 HTML 表单提交对象的 JSON 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54577231/

相关文章:

html - 关于媒体查询的问题

php - 从 53 个表返回 mysql 数据库的 p_id 和 t_id 记录?

ios - 这是我的 HTTP POST 请求,但我成功获得了连接。它显示响应为空。如何纠正这个?

jquery - 使用 JQuery.getJSON 指定回调参数

html - CSS 按钮未对齐

jQuery UI 自动完成不过滤数据

javascript - 无法在 JavaScript 中使用此选择功能

jquery - 鼠标悬停时交叉淡入淡出 div

css - 960px 宽度响应式 Bootstrap 容器 rails 应用程序

javascript - 如何使用 javascript 'addEventlistner(' click')' 函数长时间更改 css 样式?