如何从此嵌套数组中提取数据

时间:2018-03-18 15:37:03

标签: arrays json perl multidimensional-array data-extraction

我试图从我的PHP Web应用程序生成的JSON字符串中提取数据。

我尝试过的事情:

sub SqlQuery {

    use strict;
    use warnings;

    $SqlQuery = shift;

    my $MYSQLDB     = "db";
    my $SQLuser     = "user";
    my $SQLpassword = "password";

    my $dsn = "DBI:mysql:database = $MYSQLDB;host=localhost;port=3306";
    my $dbh = DBI->connect($dsn, $SQLuser, $SQLpassword);

    my $DrugsJson = '';
    my @myjson;

    say "Query is " . $SqlQuery;

    my $rows = $dbh->selectall_arrayref(
        $SqlQuery, { Slice => {} }
    );

    my $ct = 0;

    foreach my $row ( @$rows ) {

        say "Count: $ct";

        $DrugsJson = $row->{medicine};
        say $DrugsJson;

        @myjson = decode_json($DrugsJson);
        say Dumper @myjson;

        foreach my $row (@myjson) {

            say "Row is " .ref $row;

            foreach my $element ( @$row ) {

                say "Element is " .ref $element;

                foreach my $subelement (@{$element}) {
                    say "SubElement is " . $subelement;
                }
            }
        }

        foreach ($myjson[0][0]) {
            push @Brand, $_;
        }

        say "Listing brands..";
        say Dumper @Brand;
    }
}

我试图将包含"NASOCLEAR NASAL SPRAY","ASCORIL FLU SYRUP","CALPOL 120MG SUSPENSION"的数据读入数组@Brand,将"SODIUM CHLORIDE 0.65%W/V","CHLORPHENIRAMINE 2MG+PHENYLEPHRINE 5MG","PARACETAMOL 120MG/5ML"读入@Generic,依此类推。

示例输出:

Query is SELECT `diagnosis`, `ICDCode`, `ICDVer`, `history`, `examination`, `medicine`, `investigation`, `consultations` FROM `clinical` WHERE `CheckinNo`=85 LIMIT 1
Count: 0
[["NASOCLEAR NASAL SPRAY","ASCORIL FLU SYRUP","CALPOL 120MG SUSPENSION"],["SODIUM CHLORIDE 0.65%W/V","CHLORPHENIRAMINE 2MG+PHENYLEPHRINE 5MG","PARACETAMOL 120MG/5ML"],["1","3","5"],["Drops","mL","mL"],["TDS","BD","TDS"],["5","5","3"],["days","days","days"],"85"]
$VAR1 = [
          [
            'NASOCLEAR NASAL SPRAY',
            'ASCORIL FLU SYRUP',
            'CALPOL 120MG SUSPENSION'
          ],
          [
            'SODIUM CHLORIDE 0.65%W/V',
            'CHLORPHENIRAMINE 2MG+PHENYLEPHRINE 5MG',
            'PARACETAMOL 120MG/5ML'
          ],
          [
            '1',
            '3',
            '5'
          ],
          [
            'Drops',
            'mL',
            'mL'
          ],
          [
            'TDS',
            'BD',
            'TDS'
          ],
          [
            '5',
            '5',
            '3'
          ],
          [
            'days',
            'days',
            'days'
          ],
          '85'
        ];

Row is ARRAY
Element is ARRAY
SubElement is NASOCLEAR NASAL SPRAY
SubElement is ASCORIL FLU SYRUP
SubElement is CALPOL 120MG SUSPENSION
Element is ARRAY
SubElement is SODIUM CHLORIDE 0.65%W/V
SubElement is CHLORPHENIRAMINE 2MG+PHENYLEPHRINE 5MG
SubElement is PARACETAMOL 120MG/5ML
Element is ARRAY
SubElement is 1
SubElement is 3
SubElement is 5
Element is ARRAY
SubElement is Drops
SubElement is mL
SubElement is mL
Element is ARRAY
SubElement is TDS
SubElement is BD
SubElement is TDS
Element is ARRAY
SubElement is 5
SubElement is 5
SubElement is 3
Element is ARRAY
SubElement is days
SubElement is days
SubElement is days
Element is 
Can't use string ("85") as an ARRAY ref while "strict refs" in use at ./sendpresc.pl line 121.

1 个答案:

答案 0 :(得分:0)

在调用decode_json之后,您获得的JSON在Perl中被转换为数组引用的数组引用,用于所有行但最后一行(或者在注释中提出建议"引用一个数组引用数组"),包含一个标量(行id)。要遍历JSON,您可以这样做:

use strict;
use warnings;

use JSON;

my $DrugsJson = '[["NASOCLEAR NASAL SPRAY","ASCORIL FLU SYRUP","CALPOL 120MG SUSPENSION"],["SODIUM CHLORIDE 0.65%W/V","CHLORPHENIRAMINE 2MG+PHENYLEPHRINE 5MG","PARACETAMOL 120MG/5ML"],["1","3","5"],["Drops","mL","mL"],["TDS","BD","TDS"],["5","5","3"],["days","days","days"],"85"]';

my $myjson = decode_json($DrugsJson);

# Traverse the JSON
foreach my $i ( @$myjson ) {
  print "Row is ", ref $i, "\n";
  if( ref $i eq 'ARRAY' ) {
    foreach my $j ( @$i ) {
      print "Sub-element is ", $j, "\n";
    }
  }
  else {
    print "Element is ", $i, "\n";
  }
}

# Create arrays for Brand and Generic
my @Brand   = @{ $myjson->[0] };
my @Generic = @{ $myjson->[1] };

print 'Brand has: ', join(', ', @Brand), "\n";
print 'Generic has: ', join(', ', @Generic), "\n";

我创建了一些代码,让JSON变成了Perl数据结构(我没有你的数据库。)最好在像$myjson = decode_json($DrugsJson)这样的标量变量中接收JSON。从那里,ref会告诉你你所拥有的是一个参考(一个带有&#34的字符串; ARRAY"," HASH"等等)或一个标量(没有值)

为了解决数组引用的数组引用的元素$i, $j,您可以使用$myjson->[$i][$j],它是$myjson->[$i]->[$j]的紧凑形式。您还可以使用转换@{ $array_ref }将数组引用转换为数组。对于简单表达式,您还可以使用@$array_ref。但是对于数组引用的元素,您需要更长的@{ $myjson->[0] }

输出:

Row is ARRAY
Sub-element is NASOCLEAR NASAL SPRAY
Sub-element is ASCORIL FLU SYRUP
Sub-element is CALPOL 120MG SUSPENSION
Row is ARRAY
Sub-element is SODIUM CHLORIDE 0.65%W/V
Sub-element is CHLORPHENIRAMINE 2MG+PHENYLEPHRINE 5MG
Sub-element is PARACETAMOL 120MG/5ML
Row is ARRAY
Sub-element is 1
Sub-element is 3
Sub-element is 5
Row is ARRAY
Sub-element is Drops
Sub-element is mL
Sub-element is mL
Row is ARRAY
Sub-element is TDS
Sub-element is BD
Sub-element is TDS
Row is ARRAY
Sub-element is 5
Sub-element is 5
Sub-element is 3
Row is ARRAY
Sub-element is days
Sub-element is days
Sub-element is days
Row is
Element is 85
Brand has:   NASOCLEAR NASAL SPRAY, ASCORIL FLU SYRUP, CALPOL 120MG SUSPENSION
Generic has: SODIUM CHLORIDE 0.65%W/V, CHLORPHENIRAMINE 2MG+PHENYLEPHRINE 5MG, PARACETAMOL 120MG/5ML