bigup401 has asked for the wisdom of the Perl Monks concerning the following question:

i have post and got the response very well from the server {"name":"Jerry Stephin","country":"uk","email":"brabrabra"} -------------------- Content-type: text/html Response code: 200 Content-type: text/html bt my problem is that how can i format the json response and print out the message well-formatted like this

name brbrr

email brbbr

country brbr

i tried this bt cant work

#!"C:\xampp\perl\bin\perl.exe" use LWP::UserAgent; use HTTP::Request::Common qw(POST); use JSON::XS; my $ua = new LWP::UserAgent(keep_alive=>1); my $responde = HTTP::Request->new(POST =>""); $responde->content_type("application/x-www-form-urlencoded"); $responde->content_type("Content-Type' => 'application/json"); my $responseObj = $ua->request($responde); print "Content-type: text/html\n\n"; print $responseObj->content."\n--------------------\n"; my $responseCode = $responseObj->code; print "Content-type: text/html\n\n"; print 'Response code: ' . $responseCode . "\n"; $isSuccesResponse = $responseCode < 400; my $response = JSON::XS->new->decode ($responseObj->content); if ($isSuccesResponse) { package json_res; sub new { my $class = shift; my $self = { name => shift, email => shift, country => shift, }; bless $self, $class; return $self; } sub TO_JSON { return { %{ shift() } }; } package main; use JSON; my $JSON = JSON->new->utf8; $JSON->convert_blessed(1); my $js = new json_res($response); my $json = $JSON->encode($js); sub getname { my( $self ) = @_; return $self->{name}; } sub getemail { my( $self ) = @_; return $self->{email}; } sub getcountry { my( $self ) = @_; return $self->{country}; } my $name = $json->getname(); my $email = $json->getemail(); my $country = $json->getcountry(); print "Names Is: $name\n"; print "Email Is: $email\n"; print "Country Is: $country\n"; }

Replies are listed 'Best First'.
Re: json response
by hdb (Monsignor) on Apr 12, 2015 at 09:20 UTC

    If you have a JSON string, you can translate it into a Perl structure using the from_json function which returns in your case a reference to a hash. Use Data::Dumper to inspect the structure and the retrieve the desired information:

    use strict; use warnings; use JSON; use Data::Dumper; my $json = '{"name":"Jerry Stephin","country":"uk","email":"brabrabra" +}'; my $perl = from_json $json; print Dumper $perl; print "$_ $perl->{$_}\n" for qw( name email country );
Re: json response ( unblender )
by Anonymous Monk on Apr 12, 2015 at 10:05 UTC

    i tried this bt cant work

    yes, it cannot work, too much stuff in blender order (random)

    you're trying too many things at once in a blender

    package statements do not go into if blocks

    putting code in between subroutine definitions is confusing for everyone

    using variables before they are defined doesn't work , Coping with Scoping

    strict+warnings complains about using vars before they are defined

    make more subs, make your program readable

    make skimmable code :)

    So you write

    #!"C:\xampp\perl\bin\perl.exe" -- #!/usr/bin/perl -- ## funstuff.cgi ## 2015-04-12-03:02:07 ## ## ## ## ## perltidy -olq -csc -csci=3 -cscl="sub : BEGIN END " -otr -opr -ce +-nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while " -otr + -opr -ce -nibc -i=4 -pt=0 "-nsak=*" ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if while for " +-otr -opr -ce -nibc -i=4 -pt=0 "-nsak=*" #!/usr/bin/perl -- use strict; use warnings; use CGI::Carp qw/ fatalsToBrowser /; use CGI; ... Main( @ARGV ); exit( 0 ); sub Main { my( $headers, $body ) = FunStuff(); print $headers, $body; } ## end sub Main sub FunStuff { my $json = JsonRes->new( RequestSomeJson() ); my $headers = CGI->new->header; my $body = $json->valuesIs; return $headers, $body; } ## end sub FunStuff sub RequestSomeJson { my $ua = ... ; return JSON::XS->new->decode( $responseObj->content ); } ## end sub RequestSomeJson { package JsonRes; sub valuesIs { my( $self ) = @_; return join '', "Names Is: $name\n", "Email Is: $email\n", "Country Is: $country\n"; } ## end sub valuesIs sub new { my $class = shift; my $self = { name => shift, email => shift, country => shift, }; bless $self, $class; return $self; } ## end sub new sub getname { my( $self ) = @_; return $self->{name}; } ## end sub getname sub getemail { my( $self ) = @_; return $self->{email}; } ## end sub getemail sub getcountry { my( $self ) = @_; return $self->{country}; } ## end sub getcountry }

    Problem with JsonRes? You delete/remove all the CGI/LWP stuff from the program and only deal with JsonRes

    Problem with LWP? You delete/remove all the CGI/JsonRes stuff and deal only with LWP

    Problem with CGI? You delete/remove all the LWP/JsonRes stuff and deal only with LWP

    You have to fix one thing at a time before you try to fix the whole thing

      Wow it was just a simple logic lol!

      by adding

      $x = $response->{name}; $j = $response->{email}; $k = $response->{country}; print "Name Is: $x\n"; print "Email Is: $j\n"; print "Country Is: $k\n";
      Content-type: text/html Response code: 200 Name Is: Jerry Stephin  Email Is: brabrabra Country: Uk

      i got nice output, i think i was tired i never think about it at first. i wouldn't have disturb u guys