Perl将数据转换为JSON格式
我在将数据转换为 json 时遇到问题,我不知道为什么。
下面是一些有效的代码:
#constructor
sub new {
my $class = shift;
my $Titel = shift;
my $Text = shift;
my $Time = localtime;
my $self = {};
$self->{text} = $Text;
$self->{create} = $Time;
$self->{edit} = $Time;
my $json = JSON::XS->new();
open WF, '>> $file' || die "Error : $!";
print WF $json->encode($self)."\n";
close WF;
bless $self, $class;
}
我创建了一个“对象”并将数据保存在文本文件中(通过 JSON)。
如果我尝试编辑一些数据,我会遇到问题:
sub edit {
my $self = shift;
my $Text = shift;
my $ID = shift;
my $Time = localtime;
my $json = JSON::XS->new();
$json->allow_blessed(1);
$self->{text} = $Text; #edit text
$self->{edit} = $Time; # edit date
open INPUT, '< $file' || die "Error : $!";
my @data = <INPUT>;
close(INPUT);
open WF, '> $file' || die "Error : $!";
for (my $Count=0; $Count<=$#data; $Count++){
chomp($data[$Count]);
if($Count == $ID){#if line of data found, who is going to be edited
print WF $json->encode($self)."\n";
}else{
print WF $data[$Count]."\n";
}
}
close WF;
}
我尝试做的是仅编辑文本文件中的一行..(如果您有更好的想法,请告诉我:D)
我认为我的程序之间没有区别第一个显示的代码和那个代码......
它只是在文本文件中写回“null”......
有什么想法吗?
I have a problem, converting my data into json, and I don't know why.
Here is some Code that works:
#constructor
sub new {
my $class = shift;
my $Titel = shift;
my $Text = shift;
my $Time = localtime;
my $self = {};
$self->{text} = $Text;
$self->{create} = $Time;
$self->{edit} = $Time;
my $json = JSON::XS->new();
open WF, '>> $file' || die "Error : $!";
print WF $json->encode($self)."\n";
close WF;
bless $self, $class;
}
I create an 'object' and save the data in a textfile (via JSON), too.
I have problems, if I try to edit some data:
sub edit {
my $self = shift;
my $Text = shift;
my $ID = shift;
my $Time = localtime;
my $json = JSON::XS->new();
$json->allow_blessed(1);
$self->{text} = $Text; #edit text
$self->{edit} = $Time; # edit date
open INPUT, '< $file' || die "Error : $!";
my @data = <INPUT>;
close(INPUT);
open WF, '> $file' || die "Error : $!";
for (my $Count=0; $Count<=$#data; $Count++){
chomp($data[$Count]);
if($Count == $ID){#if line of data found, who is going to be edited
print WF $json->encode($self)."\n";
}else{
print WF $data[$Count]."\n";
}
}
close WF;
}
What I try to do is to edit just one line in the textfile.. (if you have a better idea, please show me :D)
I see no difference between my procedure in the code shown first and that one....
it just writes "null" back in the textfile...
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不是 JSON 专家,但是
encode
方法在使用受祝福的引用时遇到了问题。使用 unblessed 引用似乎是一个有效的解决方法:I'm no JSON expert, but the
encode
method is having trouble with a blessed reference. Using an unblessed reference seems like a valid workaround:我赞同这个观点(正如您已经发现的那样),问题是受祝福的参考,但是我为您提供了另一个解决方案(毕竟是 Perl:TIMTOWTDI)。 Acme::Damn 模块允许您取消祝福(即诅咒)对象。因此,您应该能够:
而且我觉得我必须分享,因为该方法的命名如此巧妙。
I second the notion (as you have already found) that the problem is the blessed reference, however I offer you another solution (the is Perl after all: TIMTOWTDI). The Acme::Damn module allows you to unbless (i.e. damn) an object. Therefore you should be able to:
Also I felt I had to share, since the method is so cleverly named.
根据最后一个暴民的建议,这里是如何序列化受祝福的引用的简单示例。
不过解码时需要注意。可以使用 filter_json_single_key_object 实现完整的往返。
Following the last mob's suggestion, here is simple example how to serialize blessed references.
However you need to pay attention in decoding. It is possible to use filter_json_single_key_object to implement full round-trip.