[英]parse form (POST) data to create a multidimensional hash
I have a form which is used to submit data about multiple people. 我有一个用于提交有关多个人的数据的表格。 There are multiple properties for each person, and I'm grouping them like the following: 每个人都有多个属性,我将它们分组如下:
<input type=hidden name="person1[firstname]" value='Sam'/>
<input type=hidden name="person1[lastname]" value='Higgins'/>
<input type=hidden name="person2[firstname]" value='Jiminy'/>
<input type=hidden name="person2[lastname]" value='Cricket'/>
...etc
When I do the following: 当我执行以下操作时:
my %hash = params;
die Dumper \%hash;
I get: 我得到:
VAR1 = {
'person1[firstname]' => 'Sam',
'person1[lastname]' => 'Higgins',
'person2[firstname]' => 'Jiminy',
'person2[lastname]' => 'Cricket',
};
When I was expecting something like: 当我期待类似的东西:
VAR1 = {
'person1' => { firstname => 'Sam', lastname => 'Higgens' },
'person2' => { firstname => 'Jiminy', lastname => 'Cricket' },
};
Is there a way to get the above, or am I doing it wrong in my HTML? 有没有办法做到以上几点,或者我在HTML中做错了吗?
Edit 编辑
I've also tried with empty brackets at the end: 我也尝试在最后用空括号括起来:
<input type=hidden name="person1[firstname][]" value='Sam'/>
but that just gave: 但这只是给了:
'person1[firstname][]' => 'Sam',
#!/usr/bin/perl
use Data::Dumper;
my $orginal = {
'person1[firstname]' => 'Sam',
'person1[lastname]' => 'Higgins',
'person2[firstname]' => 'Jiminy',
'person2[lastname]' => 'Cricket',
};
my $result = {};
foreach my $key (keys %$orginal)
{
$value = $orginal->{$key};
$key =~ m/^(.*)\[(.*)\]$/;
#$1 = for example person1
#$2 = forexample firstname
$result->{$1}->{$2} = $value;
}
print Dumper($result);
#RESULT:
# $VAR1 = {
# 'person1' => {
# 'firstname' => 'Sam',
# 'lastname' => 'Higgins'
# },
# 'person2' => {
# 'firstname' => 'Jiminy',
# 'lastname' => 'Cricket'
# }
# };
To answer the question more wholly than providing a link: 要比提供链接更完整地回答问题:
I went with a solution that uses jQuery, specifically using a plugin ( https://github.com/marioizquierdo/jquery.serializeJSON ), to send the data asynchronously (AJAX) and using Dancers from_json
method which creates a hashref of the JSON string . 我提供了一个使用jQuery的解决方案,特别是使用了一个插件( https://github.com/marioizquierdo/jquery.serializeJSON ),以异步方式(AJAX)发送数据,并使用了Dancers from_json
方法,该方法创建了JSON 字符串的hashref 。
I emphasise string because the function offered by the serializeJSON plugin creates a JSON object, and this Dancer does not convert to the proper structure. 我强调字符串是因为serializeJSON插件提供的功能会创建一个JSON对象,并且该Dancer不会转换为正确的结构。 Therefore you need to use JSON.stringify()
to create a json string, which Dancer does accept :) 因此,您需要使用JSON.stringify()
创建一个JSON字符串,Dancer会接受:)
Below is the code: 下面是代码:
Example HTML: HTML示例:
<input type=hidden name="person1[firstname]" value='Sam'/>
<input type=hidden name="person1[lastname]" value='Higgins'/>
JS (jQuery): JS(jQuery):
var formData = $(this).serializeJSON();
console.log(formData);
$.ajax({
'url': '/your_url',
'type': 'POST',
'data': JSON.stringify(formData),
'success': function(res){
console.log(res);
}
});
Perl (Dancer): Perl(Dancer):
post '/your_url' => sub {
my $json = request->body;
use Data::Dumper;
my $hashref = {};
$hashref = from_json($json);
die Dumper \$hashref->{person1}->{name}; # 'Sam'
}
Thanks to all who helped! 感谢所有的帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.