Perl字符串到Json

时间:2015-08-24 05:40:24

标签: json perl

Router.route('/path', {
  name: "waiting_route_name",
  waitOn: function(){
    return Meteor.subscribe('yoursub', function post_process_onready(){
        //post processing here
    });
  },
  loadingTemplate: 'loading',
  action: function(){
    this.render("the_template_name")
  }
})

我尝试使用上面的示例将package Emp; sub new { my $class = shift; my $self = { OrderID => shift, OrderDate => shift, CustomerID => shift, ShipName => shift, Freight => shift, }; bless $self, $class; return $self; } sub TO_JSON { return { %{ shift() } }; } package main; use JSON; my $JSON = JSON->new->utf8; $JSON->convert_blessed(1); $e = new Emp( "10248", "1996-07-04", "WILMK","Vins","10"); $json = $JSON->encode($e); print "$json\n"; 转换为String。输出如下:

JSON

如果我希望我的JSON如下所示,我在哪里更改:

{"Freight":"10","OrderDate":"1996-07-04","CustomerID":"WILMK","OrderID":"10248","ShipName":"Vins"}

非常感谢任何建议或参考链接。

1 个答案:

答案 0 :(得分:3)

创建您要查找的数据结构,然后调用JSON-> encode

main 包中,尝试以下操作:

use JSON;

my $JSON  = JSON->new->utf8;
$JSON->convert_blessed(1);

my $data = { rows => [] };
push @{$data->{rows}}, new Emp( "10248", "1996-07-04", "WILMK","Vins","32");
push @{$data->{rows}}, new Emp( "10276", "1996-08-08", "TORTU","Tortuga","13");
push @{$data->{rows}}, new Emp( "10277", "1996-08-09", "MORGK","Morgenstern","125");

$json = $JSON->encode($data);
print "$json\n";

<强>输出:

{"rows":[{"Freight":"32","OrderDate":"1996-07-04","CustomerID":"WILMK","OrderID":"10248","ShipName":"Vins"},
{"Freight":"13","OrderDate":"1996-08-08","CustomerID":"TORTU","OrderID":"10276","ShipName":"Tortuga"},
{"Freight":"125","OrderDate":"1996-08-09","CustomerID":"MORGK","OrderID":"10277","ShipName":"Morgenstern"}]}