1
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的Perl字符串
{"Freight":"10","OrderDate":"1996-07-04","CustomerID":"WILMK","OrderID":"10248","ShipName":"Vins"}
我在哪裏可以改變,如果我想我的JSON是象下面這樣:
{
"rows":[
{"OrderID":"10248","OrderDate":"1996-07-04","CustomerID":"WILMK","ShipName":"Vins et alcools Chevalier","Freight":"32.3800"},
{"OrderID":"10276","OrderDate":"1996-08-08","CustomerID":"TORTU","ShipName":"Tortuga Restaurante","Freight":"13.8400"},
{"OrderID":"10277","OrderDate":"1996-08-09","CustomerID":"MORGK","ShipName":"Morgenstern Gesundkost","Freight":"125.7700"}
]
}
任何意見或引用鏈接表示高度讚賞。