在下面的示例中,asn1 定义有两个标签 schoolId 和 student。标签 student 是 SEQUENCE 的 SEQUENCE。我从 JSON 格式的文件中获取学生信息,例如:
{"school1": [{"name": "jason","id": 12345, "age": 14}, {"name": "karen","id": 12346, "age": 14}, {“姓名”:“迈克”,“身份证”:12347,“年龄”:15}]}
{“school2”:[{“name”:“dave”,“id”:12645,“age”:16},{“name”:“angel”,“id”:12346,“age”:14}, {“姓名”:“苏珊”,“身份证”:12347,“年龄”:15}]}
所以每所学校的学生人数都不一样。我的问题是如何编码学生标签,因为它有未指定数量的学生。谢谢你的帮助!
use Convert::ASN1;
use Data::Dumper;
my %scope = qw(base 0 one 1 single 1 sub 2 subtree 2);
my %deref = qw(never 0 search 1 find 2 always 3);
my $search_pdu = Convert::ASN1->new;
$search_pdu->prepare(q(
SEQUENCE {
schoolId INTEGER,
student [0] SEQUENCE OF SEQUENCE {
name STRING,
id INTEGER,
age INTEGER
}
}
)) or die $search_pdu->error;
$buf = $search_pdu->encode(
schoolId => 1001,
student => [
{
name => "Jason Howard",
id => 310089,
age => 14
},
{
name => " Angel Disante",
id => 310456,
age => 15
}
]
);
$Data::Dumper::Indent = 1;
print Dumper( $search_pdu->decode($buf));
$h = unpack("H*",$buf);
print $h,"\n";
我的问题,例如,如果 school1 有 100 名学生,school2 有 300 名学生,.......,有没有办法在编码函数中使用 for 循环来以编程方式计算学生人数?
$student_json = "{"school-1001": [{"name": "jason","id": 12345, "age": 14}, {"name": "karen","id": 12346, "age": 14},{"name": "mike","id": 12347, "age": 15}]}";
student => [
for my $hashref (@{$student_json}) {
{
name => $hashref->{'name'},
id => $hashref->{'id'},
age => $hashref->{'age'}
}
]
我尝试了许多方法来使用如上所示的 for 循环以编程方式进行编码,但总是失败。