Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 使用Perl从复杂字符串中分离变量_String_Facebook_Perl - Fatal编程技术网

String 使用Perl从复杂字符串中分离变量

String 使用Perl从复杂字符串中分离变量,string,facebook,perl,String,Facebook,Perl,我有一个类似这样的字符串,从Facebook API调用返回 "'link' => 'https://www.facebook.com/app_scoped_user_id/123456789/', 'timezone' => '5.5', 'name' => 'John Smith', 'locale' => 'en_GB', 'last_name' => 'Smith', 'email' => 'myemail@hotmail.com', 'updated_time' => '2015-05

我有一个类似这样的字符串,从Facebook API调用返回

"'link' => 'https://www.facebook.com/app_scoped_user_id/123456789/', 'timezone' => '5.5', 'name' => 'John Smith', 'locale' => 'en_GB', 'last_name' => 'Smith', 'email' => 'myemail@hotmail.com', 'updated_time' => '2015-05-05T15:35:31+0000', 'verified' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ), 'id' => '123456789', 'birthday' => '05/01/1956', 'first_name' => 'John', 'gender' => 'male'" 等等

花了好几个小时才从Facebook中提取出字符串,现在我正努力使用子字符串或哈希来分离变量。

代码>。。。bless(do{(my$o=1)},'JSON::PP::Boolean'),…是一个赠品,表明这是
Data::Dumper
输出,它已经是一个Perl可读的表达式。把它用大括号括起来,
eval
it,你就得到了一个散列引用

my $fb_output = "'link' => ...  'gender' => 'male'";
my $hashref = eval "{$fb_output}";
print "The last name is ", $hashref->{last_name}, "\n";
...

在代码的上一两级,您可能从Facebook接收到一个JSON字符串,将其解码为哈希引用,然后使用
Data::Dumper
将哈希引用转换为上述字符串。那将是处理数据的更合适的地方。

Hi。是的,非常有洞察力!事实正是如此。然而,我发现Facebook似乎没有返回正确的JSON字符串——我在尝试使用JSON解析它时出错,发现其他人也有类似的问题。因此,我摆弄了一下Dumper,现在尝试手动解析字符串。我承认不漂亮。感谢您的投入和指导。我会继续努力解决这个问题!在你的帮助下,它开始工作了:$user=$fb->fetch('me')$字符串=转储程序$user$长度=长度($string)$string=substr$string,7,$length-7-2;#这将删除不必要的内容$hashref=eval“{$string}”;打印“姓氏是”,$hashref->{last_name},“\n”@Scotsman:从这一点来看,它看起来肯定不是一个字符串,而是与您最终得到的数据结构完全相同的数据结构(因此fetch已经在进行json解码);只要做
$user->{last_name}
!另外,substr可以从末尾取一个偏移量,所以
substr$string,7,-2
What@ysth说的。返回格式为JSON的
fetch
方法——您不应该尝试使用字符串函数来处理它,而应该使用Perl提供的JSON解析功能。
my $fb_output = "'link' => ...  'gender' => 'male'";
my $hashref = eval "{$fb_output}";
print "The last name is ", $hashref->{last_name}, "\n";
...