我向 vk.com('wall.get')发出一个API请求,它以JSON格式返回响应。
我使用的模块:
use VK::App;
use JSON qw( decode_json );
use Data::Dumper;
use JSON::XS;
完整响应:
{"response":[1696,{"id":9298,"from_id":-95784908,"to_id":-95784908,"date":1468314922,"marked_as_ads":0,"post_type":"post","text":"Пока рэпера придумывaют нoвые истории,<br>О чем же им написaть я выпью алкоголя.","signer_id":305317129,"can_edit":1,"created_by":305317129,"can_delete":1,"can_pin":1,"media":{"type":"audio","owner_id":2000355235,"item_id":456239648},"attachment":{"type":"photo","photo":{"pid":426327566,"aid":-7,"owner_id":-95784908,"user_id":100,"src":"https://pp.vk.me/c633131/v633131850/39ece/lnz0Bz1WuLs.jpg","src_big":"https://pp.vk.me/c633131/v633131850/39ecf/yRnLIcZbUNI.jpg","src_small":"https://pp.vk.me/c633131/v633131850/39ecd/EESnBXYilMI.jpg","src_xbig":"https://pp.vk.me/c633131/v633131850/39ed0/RNR8FS9CNjg.jpg","src_xxbig":"https://pp.vk.me/c633131/v633131850/39ed1/pXN8-5o7szc.jpg","width":960,"height":959,"text":"","created":1468281534,"access_key":"fef98dd6d847faf868"}},"attachments":[{"type":"photo","photo":{"pid":426327566,"aid":-7,"owner_id":-95784908,"user_id":100,"src":"https://pp.vk.me/c633131/v633131850/39ece/lnz0Bz1WuLs.jpg","src_big":"https://pp.vk.me/c633131/v633131850/39ecf/yRnLIcZbUNI.jpg","src_small":"https://pp.vk.me/c633131/v633131850/39ecd/EESnBXYilMI.jpg","src_xbig":"https://pp.vk.me/c633131/v633131850/39ed0/RNR8FS9CNjg.jpg","src_xxbig":"https://pp.vk.me/c633131/v633131850/39ed1/pXN8-5o7szc.jpg","width":960,"height":959,"text":"","created":1468281534,"access_key":"fef98dd6d847faf868"}},{"type":"audio","audio":{"aid":456239648,"owner_id":2000355235,"artist":"Костик ИзХабарэ","title":"ЗаМКАДыш","duration":211,"url":"https://cs1-43v4.vk-cdn.net/p14/5c654491198674.mp3?extra=alCnXJlE-kVhqAR-FhYkeLc3VfVrav607YQqc6RhT--YU-qDG_HlcajsiUqvu9t746CXm57LkPkPLrlG98-Ay72LgAiHjAjBiw1ziotmhGH4n6s9zcs4VPGpGUpgDnhmkHWGxYoG4Nwgt2Q","performer":"Костик ИзХабарэ","album":"2","genre":18}}],"post_source":{"type":"vk"},"comments":{"count":0,"can_post":1},"likes":{"count":0,"user_likes":0,"can_like":1,"can_publish":1},"reposts":{"count":0,"user_reposted":0},"online":0,"reply_count":0}]}
我需要解析"text =>"之后的文本
在数据之后::D umper;
$VAR1 = {
'response' => [
1696,
{
'comments' => {
'can_post' => 1,
'count' => 2
},
'attachments' => [
{
'type' => 'photo',
'photo' => {
'aid' => -7,
'lat' => '55.836657',
'created' => 1468232107,
'height' => 719,
'owner_id' => -95784908,
'text' => '',
'long' => '37.38596',
'src_big' => 'https://pp.vk.me/c633917/v633917838/3d4e7/8uk9QZ0X0ew.jpg',
'src' => 'https://pp.vk.me/c633917/v633917838/3d4e6/JwQKaOWs2zM.jpg',
'access_key' => '290371f54b8b5df024',
'src_xbig' => 'https://pp.vk.me/c633917/v633917838/3d4e8/4hqv0_AH6wE.jpg',
'width' => 960,
'pid' => 426222993,
'post_id' => 9262,
'src_xxbig' => 'https://pp.vk.me/c633917/v633917838/3d4e9/u4oI4yaWF50.jpg',
'src_small' => 'https://pp.vk.me/c633917/v633917838/3d4e5/oq8kQ_qt5cM.jpg',
'user_id' => 100
}
}
],
'post_type' => 'post',
'to_id' => -95784908,
'created_by' => 57565838,
'attachment' => {
'type' => 'photo',
'photo' => {
'long' => '37.38596',
'text' => '',
'src_big' => 'https://pp.vk.me/c633917/v633917838/3d4e7/8uk9QZ0X0ew.jpg',
'src' => 'https://pp.vk.me/c633917/v633917838/3d4e6/JwQKaOWs2zM.jpg',
'access_key' => '290371f54b8b5df024',
'src_xbig' => 'https://pp.vk.me/c633917/v633917838/3d4e8/4hqv0_AH6wE.jpg',
'lat' => '55.836657',
'aid' => -7,
'created' => 1468232107,
'height' => 719,
'owner_id' => -95784908,
'src_xxbig' => 'https://pp.vk.me/c633917/v633917838/3d4e9/u4oI4yaWF50.jpg',
'src_small' => 'https://pp.vk.me/c633917/v633917838/3d4e5/oq8kQ_qt5cM.jpg',
'user_id' => 100,
'width' => 960,
'pid' => 426222993,
'post_id' => 9262
}
},
'likes' => {
'count' => 0,
'can_publish' => 1,
'user_likes' => 0,
'can_like' => 1
},
'can_delete' => 1,
'text' => "x{41a}x{43e}x{43c}x{443} x{43d}x{443}x{436}x{43d}x{43e}? x{423} x{43c}x{435}x{43d}x{44f} 15 x{431}x{438}x{43b}x{435}x{442}x{43e}x{432})",
'online' => 1,
'id' => 9263,
'from_id' => -95784908,
'media' => {
'item_id' => 426222993,
'type' => 'photo',
'thumb_src' => 'https://pp.vk.me/c633917/v633917838/3d4e6/JwQKaOWs2zM.jpg',
'owner_id' => -95784908
},
'date' => 1468232695,
'reposts' => {
'count' => 0,
'user_reposted' => 0
},
'marked_as_ads' => 0,
'can_edit' => 1,
'can_pin' => 1,
'post_source' => {
'type' => 'vk'
},
'signer_id' => 57565838,
'reply_count' => 2
}
]
};
my $msg = GetWallPost(); # request
$msg = Dumper($msg);
print $msg;
如果尝试使用 JSON 进行解析; 或使用 JSON::XS;
my $msg = GetWallPost();
my $decode= JSON->new->decode($msg); or
my $decode = JSON::XS->new->utf8->decode ($msg); or
my $decode = decode_json($msg);
print $decode->{'response'}[0]{'test'}; get an err ->
Can't use string ("1702") as a HASH ref while "strict refs" in use at ./testhash.pl line 47.
那么如果我尝试这个 ->
print $decode->{'response'}{'text'}; get an err ->
Not a HASH reference at ./testhash.pl line 47.
如果我在数据之后对$msg做同样的事情::D umper;我得到这个错误 ->
Can't use string ("$VAR1 = {
'response' ="...) as a HASH ref while "strict refs" in use at ./testhash.pl line 42.
主要问题 - 如何解析"文本"之后的数据,我需要$msg中的明文。谢谢
但是当然。 ->response
本质上是一个数组,如 JSON 中的[]
所示。你需要$decode->{response}[1]{text}
.