格式错误的 json 字符串 perl,youtube api 密钥

Twi*_*ler 0 perl json decode

我收到一个错误,我无法找到解决方案......我已经花了几个小时,但还没有找到任何解决办法。你能帮我一下吗 ?它在 perl 中,这是我正在使用的代码。

method getMusicInformation($strMusicID) {
    my $strLink = "https://www.googleapis.com/youtube/v3/videos?id=YqeW9_5kURI&key=AIzaSyBpzQDzTu7e59mxD9HxYP3MTdlCUWzuirQ&part=snippet";
    my $strDetails = get($strLink);
    my $arrDetails = decode_json($strDetails);
    while (my($key, $value) = each(%{$arrDetails})) {
        if (ref($value) eq 'ARRAY') {
            while (my($second_key, $second_value) = each(@{$value})) {
                return $second_value;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

}

我在控制台中出现了错误:

Error: malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "(end of string)") at Server/Systems/Music.pm line 38.
Run Code Online (Sandbox Code Playgroud)

第 38 行是:

my $arrDetails = decode_json($strDetails);
Run Code Online (Sandbox Code Playgroud)

谢谢你的理解。

Sch*_*ern 5

问题是你没有从get. 查询失败,您没有检查错误。(别担心,我也花了一段时间才弄明白这一点)。线索是at character offset 0指字符串的开始。

LWP::Simple 太简单了,不支持错误检查。相反,使用完整的 LWP::UserAgent。幸运的是,它变得更容易使用

use LWP::UserAgent;
use Carp;

...

my $ua = LWP::UserAgent->new;
my $response = $ua->get($strLink);
if( !$response->is_success ) {
    croak "Fetching $strLink failed: ".$response->status_line;
}

my $arrDetails = decode_json($response->decoded_content);
Run Code Online (Sandbox Code Playgroud)

就我而言,问题是这样的:

Fetching https://www.googleapis.com/youtube/v3/videos?id=YqeW9_5kURI&key=AIzaSyBpzQDzTu7e59mxD9HxYP3MTdlCUWzuirQ&part=snippet failed: 501 Protocol scheme 'https' is not supported (LWP::Protocol::https not installed) at /Users/schwern/tmp/test.pl line 15.
    main::getMusicInformation(10) called at /Users/schwern/tmp/test.pl line 30
Run Code Online (Sandbox Code Playgroud)

所以我需要安装 LWP::Protocol::https 来支持 https。你可能也会这样做。