regex - Finding a value in HTTP response content using Perl Script -
i have perl script http request. repsonse content like
$var1 = \'{"resultset": { "result": [ { "rank": "999999", "term": "shampoo" }, { "rank": "999999", "term": "beauty", "url": "/search/results.jsp?ntt=shampoo&n=359434" }, { "rank": "999999", "term": "baby, kids & toys", "url": "/search/results.jsp?ntt=shampoo&n=359449" }, i need url property above response how can it. itried using regex my $content =~ m/:"url": "(...)"/; not getting url value. please guide.
that json. use json module parse it:
use json; $json = decode_json ( $response -> content ); foreach $element ( @{ $json -> {resultset} -> {results} } ) { print $element -> {url},"\n"; } fuller; runnable example:
#!/usr/bin/perl use strict; use warnings; use json; use data::dumper; $json_str = '{ "resultset": { "result": [ { "rank": "999999", "term": "shampoo" }, { "rank": "999999", "term": "beauty", "url": "/search/results.jsp?ntt=shampoo&n=359434" }, { "rank": "999999", "term": "baby, kids & toys", "url": "/search/results.jsp?ntt=shampoo&n=359449" } ] }}'; $json = decode_json($json_str); print dumper $json; foreach $element ( @{ $json->{resultset}->{result} } ) { print $element ->{url}, "\n" if $element->{url}; } in above, $json_str fills niche of content. i've made assumption have plain text, , output above result of print dumper \$content.
this prints:
$var1 = { 'resultset' => { 'result' => [ { 'rank' => '999999', 'term' => 'shampoo' }, { 'rank' => '999999', 'term' => 'beauty', 'url' => '/search/results.jsp?ntt=shampoo&n=359434' }, { 'url' => '/search/results.jsp?ntt=shampoo&n=359449', 'term' => 'baby, kids & toys', 'rank' => '999999' } ] } }; /search/results.jsp?ntt=shampoo&n=359434 /search/results.jsp?ntt=shampoo&n=359449
Comments
Post a Comment