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

时间:2017-02-08 21:40:12

标签: json perl 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;
            }
        }
    }

}

我在控制台中遇到错误:

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.

第38行是:

my $arrDetails = decode_json($strDetails);

感谢您的理解。

1 个答案:

答案 0 :(得分:4)

问题是你从get得到了什么。查询失败,您没有检查错误。 (不用担心,我花了一些时间来解决这个问题)。线索是at character offset 0,意思是字符串的开头。

LWP :: Simple太简单了,不支持错误检查。相反,使用完整的LWP :: UserAgent。幸运的是it's gotten a lot easier to use

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);

就我而言,问题是:

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

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