1

在下面的示例中,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 循环以编程方式进行编码,但总是失败。

4

2 回答 2

0

给定一个包含学校和学生数据的 JSON 字符串,您可以尝试:

use feature qw(say);
use strict;
use warnings;
use Convert::ASN1;
use Data::Dumper;
use JSON::XS;

my $json = <<'END'; 
{
  "school1": [{"name": "jason","id": 12345, "age": 14}, {"name": "karen","id": 12346, "age": 14},{"name": "mike","id": 12347, "age": 15}],
  "school2": [{"name": "dave","id": 12645, "age": 16}, {"name": "angel","id": 12346, "age": 14},{"name": "susan","id": 12347, "age": 15}]
}
END

my $hash = decode_json $json;

my $search_pdu = Convert::ASN1->new;
$search_pdu->prepare(q(
  school SEQUENCE OF SEQUENCE {
    schoolId     INTEGER,
    student      SEQUENCE OF SEQUENCE {
      name       STRING,
      id         INTEGER,
      age        INTEGER
   }
}
)) or die $search_pdu->error;

my @schools;
my $data = {school => \@schools};
my $school_id = 1001;
for my $school ( keys %$hash ) {
    my @students;
    my $item = { schoolId => $school_id, student => \@students };
    for my $student (@{$hash->{$school}}) {
        push @students, $student;
    }
    push @schools, $item;
    $school_id++;
} 

my $buf = $search_pdu->encode( $data );

$Data::Dumper::Indent = 1;
print Dumper( $search_pdu->decode($buf));

输出

$VAR1 = {
  'school' => [
    {
      'student' => [
        {
          'name' => 'jason',
          'id' => 12345,
          'age' => 14
        },
        {
          'name' => 'karen',
          'id' => 12346,
          'age' => 14
        },
        {
          'age' => 15,
          'id' => 12347,
          'name' => 'mike'
        }
      ],
      'schoolId' => 1001
    },
    {
      'schoolId' => 1002,
      'student' => [
        {
          'age' => 16,
          'id' => 12645,
          'name' => 'dave'
        },
        {
          'name' => 'angel',
          'age' => 14,
          'id' => 12346
        },
        {
          'name' => 'susan',
          'age' => 15,
          'id' => 12347
        }
      ]
    }
  ]
};
于 2020-05-21T20:55:39.340 回答
0

我的问题,例如,如果 school1 有 100 名学生,school2 有 300 名学生,.......,有没有办法在编码函数中使用 for 循环来以编程方式计算学生人数?

 $student_json = "{"student": [{"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'}                                    
    }
]
于 2020-05-21T21:48:07.973 回答