使用 Perl 进行简单的 JSON 解析

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5210523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:47:09  来源:igfitidea点击:

Simple JSON parsing using Perl

perljson

提问by kristin

I'm trying to parse the Facebook Graph APIJSON results, and I'm having a bit of trouble with it.

我正在尝试解析 Facebook Graph APIJSON 结果,但遇到了一些麻烦。

What I was hoping to do was print the number of shares:

我希望做的是打印共享数量:

my $trendsurl = "https://graph.facebook.com/?ids=http://www.filestube.com";
my $json;
{
  local $/; #enable slurp
  open my $fh, "<", $trendsurl;
  $json = <$fh>;
}

my $decoded_json = @{decode_json{shares}};
print $decoded_json;

回答by Sdaz MacSkibbons

Some of the code above is extremely puzzling. I've just rewritten it with annotations for you.

上面的一些代码非常令人费解。我刚刚为你重写了注释。

#!/usr/bin/perl

use LWP::Simple;                # From CPAN
use JSON qw( decode_json );     # From CPAN
use Data::Dumper;               # Perl core module
use strict;                     # Good practice
use warnings;                   # Good practice

my $trendsurl = "https://graph.facebook.com/?ids=http://www.filestube.com";

# open is for files.  unless you have a file called
# 'https://graph.facebook.com/?ids=http://www.filestube.com' in your
# local filesystem, this won't work.
#{
#  local $/; #enable slurp
#  open my $fh, "<", $trendsurl;
#  $json = <$fh>;
#}

# 'get' is exported by LWP::Simple; install LWP from CPAN unless you have it.
# You need it or something similar (HTTP::Tiny, maybe?) to get web pages.
my $json = get( $trendsurl );
die "Could not get $trendsurl!" unless defined $json;

# This next line isn't Perl.  don't know what you're going for.
#my $decoded_json = @{decode_json{shares}};

# Decode the entire JSON
my $decoded_json = decode_json( $json );

# you'll get this (it'll print out); comment this when done.
print Dumper $decoded_json;

# Access the shares like this:
print "Shares: ",
      $decoded_json->{'http://www.filestube.com'}{'shares'},
      "\n";

Run it and check the output. You can comment out the print Dumper $decoded_json;line when you understand what's going on.

运行它并检查输出。print Dumper $decoded_json;当您了解正在发生的事情时,您可以注释掉该行。

回答by Kaji Lama

How about using CURL command instead? (P.S.: This is running on Windows; make CURL changes for Unix systems).

改用 CURL 命令怎么样?(PS:这是在 Windows 上运行;对 Unix 系统进行 CURL 更改)。

$curl=('C:\Perl64\bin\curl.exe -s http://graph.facebook.com/?ids=http://www.filestube.com');
$exec=`$curl`;
print "Output is::: \n$exec\n\n";

# match the string "shares": in the CURL output
if ($exec=~/"shares":?/) { 
    print "Output is:\n$exec\n";

    # string after the match (any string on the right side of "shares":)
    $shares=$'; 

    # delete all non-Digit characters after the share number
    $shares=~s/(\D.*)//; 
    print "Number of Shares is: ".$shares."\n";
} else {
    print "No Share Information available.\n"
}

OUTPUT:

输出:

Output is:
{"http:\/\/www.msn.com":{"id":"http:\/\/www.msn.com","shares":331357,"comments":19}}
Number of Shares is: 331357