Accessing values of json structure in perl

后端 未结 3 1383
醉话见心
醉话见心 2021-01-02 21:45

I have a json structure that I\'m decoding that looks like this:

  person => {
    city => \"Chicago\",
    id => 123,
    name => \"Joe Smith\",         


        
3条回答
  •  借酒劲吻你
    2021-01-02 22:46

    Assuming your $listing is a person you have to dereference array and hash refs.

    # as long as we are assuming $listing is a person
    # this goes inside the foreach you posted in your
    # question.
    
    # this will print all cats' names
    foreach my $cat ( @{ $listing->{pets}->{cats} } )
    {
        # here $cat is a hash reference
    
        say $cat->{name}; # cat's name
    }
    

    and so on for other stuff.

    To access them from the structure you can do:

    say $listing->{pets}->{cats}->[0]->{name}; # this will print 'cat1'
    

提交回复
热议问题