【问题标题】:Submit JSON Array of Objects with HTML Form使用 HTML 表单提交 JSON 对象数组
【发布时间】:2019-02-08 00:08:39
【问题描述】:

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

【问题讨论】:

  • 你可能需要一些像 JS 或 PHP 这样的脚本来做到这一点。

标签: html css json


【解决方案1】:

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

regexr.com/47uol

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

并替换为: \n{"name" =&gt; $1,

【讨论】:

    猜你喜欢
    • 2012-02-12
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2017-09-27
    相关资源
    最近更新 更多