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

时间:2019-02-07 15:53:39

标签: html css json

我有以下HTML,该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。任何帮助,将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

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

  

regexr.com/47uol

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

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