php - 将对象列表转换为 JSON
问题描述
我有一个对象 DTO 的列表,我想将其转换为 JSON 使用json_encode
对象:CouponDTO.php
class CouponDTO
{
private $id;
private $title;
}
我有一个对象数组,现在我需要将该对象数组转换为 JSON,我尝试了 json_encode,但它在空结果下方提供。
[
{},
{},
{},
{},
{}
]
下面是创建对象数组的代码:
$couponDTOs = array();
for($i = 0; $i < 5; $i++) {
$couponDTO = new CouponDTO();
$couponDTO->setId("1");
$couponDTO->setTitle("title");
array_push($couponDTOs, $couponDTO);
}
我期待这样的事情:
[
{
"id": 1,
"title": title
},
{
"id": 1,
"title": title
},
{
"id": 1,
"title": title
},
{
"id": 1,
"title": title
},
{
"id": 1,
"title": title
},
]
解决方案
推荐阅读
- node.js - Koa 无法获取 ctx.request-body 内部属性的值
- python-3.x - 为什么当我输入超出范围的新值时它会打印“无”?蟒蛇 3
- grpc - Http/2 Pusher 与 gRPC 性能
- php - PHP DOMDocument 抓取嵌套类
- javascript - ReactJS 中的滚动条
- c++ - std::multimap 的内存布局是什么?
- python - 从单击(python 模块)帮助菜单中删除“文本”
- php - 我可以使用 eloquent 连接并启动程序吗?
- python-3.7 - 如何使用 Gensim word2vec 模型预测未标记数据的情绪?
- kubernetes - Kubernetes:如何指定自动缩放初始准备?