[英]Finding a value in HTTP response content using Perl Script
I have perl script with HTTP GET request. 我有带有HTTP GET请求的perl脚本。 My repsonse content is 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 from above response how can i get it. 我需要以上响应的url属性,我该如何获取它。 Itried using regex like
my $content =~ m/:"url": "(...)"/;
使用正则表达式进行迭代,例如
my $content =~ m/:"url": "(...)"/;
〜m my $content =~ m/:"url": "(...)"/;
but i am not getting the url value. 但我没有得到url值。 Please guide.
请指导。
That is JSON. 那就是JSON。 So use the
JSON
module to parse it: 因此,请使用
JSON
模块进行解析:
use JSON;
my $json = decode_json ( $response -> content );
foreach my $element ( @{ $json -> {ResultSet} -> {results} } ) {
print $element -> {url},"\n";
}
Fuller; 更饱; runnable example:
可运行的示例:
#!/usr/bin/perl
use strict;
use warnings;
use JSON;
use Data::Dumper;
my $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"
}
]
}}';
my $json = decode_json($json_str);
print Dumper $json;
foreach my $element ( @{ $json->{ResultSet}->{result} } ) {
print $element ->{url}, "\n" if $element->{url};
}
In the above, $json_str
fills the niche of your content. 在上面,
$json_str
填补了内容的利基。 I've made the assumption that you have plain text, and the output above is the result of print Dumper \\$content
. 我假设您有纯文本,并且上面的输出是
print Dumper \\$content
。
This thus 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
You have a reference to a JSON string. 您有一个JSON字符串的引用。
First, get the JSON. 首先,获取JSON。
my $json = $$content;
If you (incorrectly) did Dumper(\\$content)
instead of Dumper($content)
, then ignore the above and use the following instead: 如果您(错误地)执行了
Dumper(\\$content)
而不是Dumper($content)
,则忽略上述Dumper($content)
,而使用以下内容:
my $json = $content; # Or just use $content where you see $json later.
Then, use a JSON parse to get the data. 然后,使用JSON解析来获取数据。
use JSON::XS qw( decode_json );
my $data = decode_json($json); # If the $json is UTF-8 (probably)
-or-
use JSON::XS qw( );
my $data = JSON::XS->new->decode($json); # If the $json is decoded (Unicode Code Points)
Now, it's easy to grab your data. 现在,轻松获取数据。
my $results = $data->{ResultSet}{result};
for my $result (@$results) {
my $url = $result->{url}
or next;
print("$url\n");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.