【问题标题】:Malformed JSON string when using perl and earthexplorer使用 perl 和 earthexplorer 时格式错误的 JSON 字符串
【发布时间】:2017-09-15 12:22:17
【问题描述】:

我目前正在与 USGS 的 EarthExplorer 合作,根据空间坐标设置一些 Landsat 场景的批量下载。他们在https://earthexplorer.usgs.gov/inventory/example/json-download_data-pl 提供了一个很棒的方便脚本来执行此操作,这很棒。我在集群上工作,尽管正确安装了所有 perl 模块,但当我运行脚本时,我得到以下输出:

Running Script...


Error: Error: malformed JSON string, neither array, object, number, string or 
atom, at character offset 0 (before "LWP will support htt...") at ./dl.pl line 182

这看起来很奇怪。作为解释,脚本以

开头
#!/usr/bin/perl

#use strict;
use warnings;
use 5.010;
use JSON;
use Scalar::Util qw(looks_like_number reftype dualvar );
use LWP::UserAgent;
use Getopt::Long qw(GetOptions);

my ($username, $password);
$username = "myusername_filled_in";
$password = "mypassword_filled_in";

GetOptions(
    'username=s' => \$username,
    'password=s' => \$password,
) or die "Error retrieving Username and Password\n";

而有问题的代码是

    $res = $response->{_content};
    $res = decode_json $res;

遵循Can't run Perl script on other computer 中非常有用的建议,我做了以下事情:

  1. 将违规代码区域中的 $response->content 更改为 $response->decoded_content( charset => 'none')

  2. Ran lwp-request https://google.com/ 刚刚拉回了一个完整的网页 - 没有错误。所以,这似乎奏效了。

  3. 尝试通过插入 print $response->decoded_content( charset => 'none'); 来查看一些调试,然后抛出错误

LWP will support https URLs if the LWP::Protocol::https module is installed.

而且,确实安装了 LWP::Protocol::https。

我觉得我必须缺少一些简单的东西——比如我如何定义我的用户名和密码(只是$username = "myusername"; 等,在声明变量之后)或其他一些愚蠢的东西。

还有其他人遇到过这种情况吗?


从下面的查询中添加输出:

$ which cpan ; head -n 1 `which cpan` ; echo 'o conf' | cpan | grep -P 'make|mbuild' ; set | grep ^PERL ; which perl ; perl -le'use LWP::Protocol::https; print "ok";'

/share/pkg/perl/5.10.1/bin/cpan
#!/share/pkg/perl/5.10.1/bin/perl
    make               [/usr/bin/make]
    make_arg           []
    make_install_arg   []
    make_install_make_command [/usr/bin/make]
    makepl_arg         []
    mbuild_arg         []
    mbuild_install_arg []
    mbuild_install_build_command [./Build]
    mbuildpl_arg       []
PERL5LIB=/home/jb92b/perl5/lib/perl5:/home/jb92b/perl5/lib/perl5:/home/jb92b/perl5/lib/perl5
PERL_LOCAL_LIB_ROOT=/home/jb92b/perl5:/home/jb92b/perl5:/home/jb92b/perl5
PERL_MB_OPT='--install_base "/home/jb92b/perl5"'
PERL_MM_OPT=INSTALL_BASE=/home/jb92b/perl5
/share/pkg/perl/5.10.1/bin/perl
ok

【问题讨论】:

  • 你能把这个 Perl 脚本的开头添加到问题中吗?
  • 请提供您声称已安装 LWP::Protocol::https 的依据。
  • 你意识到程序在命令行上接受参数吗?如果请求中有错误,则响应不包含有效 JSON 的可能性不大。我建议您在传递给decode_json 之前打印$res 的值。
  • 您的脚本使用/usr/bin/perl,但您可能使用/for /share/pkg/perl/5.10.1/bin/perl 安装它。您需要切换您的脚本使用哪个perl,或者您需要使用/for /usr/bin/perl 安装它。打电话,得走了。如果您还没有弄清楚,稍后会提供详细信息

标签: json perl landsat


【解决方案1】:

您的代码不会检查错误。你应该有类似的东西

$response->is_success()
   or die("Can't fetch X: ".$response->status_line());

您遇到的问题是 LWP::Protocol::https 没有安装。

你声称它是,但 Perl 在这里是权威的 :) 它不是由那个 Perl 安装的,你将它安装在一个非标准目录中而没有告诉 Perl 在那里寻找它,或者存在权限问题。

在这种情况下,您的脚本使用/usr/bin/perl,但您使用/for /share/pkg/perl/5.10.1/bin/perl 安装了模块。您需要切换您的脚本使用哪个perl,或者您需要使用/for /usr/bin/perl 安装模块。

【讨论】:

  • ` 数据库于 2017 年 9 月 13 日星期三 15:29:02 GMT 生成,LWP::Protocol::https 是最新的 (6.07)。 `
  • 嗯 - 但使用您的调试代码:Error: Can't fetch X: 501 Protocol scheme 'https' is not supported (LWP::Protocol::https not installed)
  • 所以?就像我说的:它不是由那个 Perl 安装的,你将它安装在一个非标准目录中,而没有告诉 Perl 在那里寻找它,或者存在权限问题。
  • 对不起,没有完全关注。我安装了它,我大约在同一时间安装的其他模块似乎工作正常。抱歉,如果我遗漏了一些明显的东西。
  • 已修复!这就是我的全部!
【解决方案2】:

“类似于我定义我的用户名和密码的方式(在声明变量之后,只需 $username = "myusername"; 等)”

您不应修改代码以添加您的用户名和密码。按照它的编写方式,对GetOptions 的调用将覆盖它们。你应该使用

program.pl --usernam myusername --password mypassword

【讨论】:

    猜你喜欢
    • 2015-03-07
    • 2016-03-16
    • 2014-04-24
    • 2015-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多