Link to home
Start Free TrialLog in
Avatar of jellydeal
jellydeal

asked on

parse json with perl

Hi,

Im trying to parse some simple json with a perl script.

Heres the Json :
[
    {
        "to_user": "1234567",
        "message": "test 1"
    },
    {
        "to_user": "345456657",
        "message": "test 2"
    },
    {
        "to_user": "45667324",
        "message": "test 3"
    }
]

Open in new window


my perl errors with "NOT A HASH REFERENCE", I obviously have my code wrong, but cant find out how to fix it!

Heres my Code :
use HTTP::Request;
use LWP::UserAgent;




my $json = encode_json \%message;
my $uri = 'http://xxxx.com/server/imessage/get_messages.php';


    my $req = HTTP::Request->new('POST', $uri);
    $req->header( 'Content-Type' => 'application/json' );
    $req->content( $json );
    my $lwp = LWP::UserAgent->new;
    $response = $lwp->request( $req );

    if ($response->decoded_content ne "")
    {
        print "[$datestring] ";
          my $decoded_json = decode_json($response->decoded_content);
        print $decoded_json->{"to_user"} ;    #ERROR line
    }

Open in new window


I understand I need to loop through the results and then display them individually but I cant figure out how :(

thank you
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jellydeal
jellydeal

ASKER

fantastic.. worked perfect.. thank you