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 | |
|
Re: json response ( unblender )
by Anonymous Monk on Apr 12, 2015 at 10:05 UTC | |
by bigup401 (Pilgrim) on Apr 12, 2015 at 11:32 UTC |