gpt4 book ai didi

json - 如何将 DBIx::Class::ResultSet 转换为 JSON

转载 作者:行者123 更新时间:2023-12-05 06:29:55 26 4
gpt4 key购买 nike

我正在尝试从 DBIx::Class::ResultSet 获取 JSON,但出现异常

encountered object 'Sql2Json::Model::DB::Book=HASH(0x6014f88)', 
but neither allow_blessed, convert_blessed nor allow_tags settings are
enabled (or TO_JSON/FREEZE method missing)

Controller 类是Books.pm:

package Sql2Json::Controller::Books;
use Moose;
use namespace::autoclean;
use JSON::XS;

BEGIN { extends 'Catalyst::Controller'; }

my $json = JSON::XS->new;

sub list : Local {
my($self, $c) = @_;
$c->stash(books_rs => $c->model('DB::Book'));
$c->stash(books => [$c->stash->{books_rs}->search({}, {order_by => 'name ASC'})]);
$c->stash(json_data => $json->convert_blessed->encode($c->stash->{books}));
$c->forward('View::JSON');
}

__PACKAGE__->meta->make_immutable;

1;

根据这个article祝福对象的编码就足够了:

$json->convert_blessed->encode($c->stash->{books})

我在这里遗漏了什么吗?

最佳答案

几乎所有时候,最好的方法是使用 get_inflated_column从查询返回的行的方法。

$books = $c->model('DB::Book');
$c->stash(json_data => [map {$_->get_inflated_columns} $books->all]);
$c->forward('View::JSON');

关于json - 如何将 DBIx::Class::ResultSet 转换为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52923935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com