doxzrb8721 2018-05-16 10:39
浏览 327

如何在json中返回嵌套对象列表?

I'm using Slim for write a little rest api, and I have the following situation: I need to return a list of competitions which have different seasons available, now if I do this:

$app->get('/get_competitions', function (Request $request, Response $response, array $args)
{
  $sql = $this->db->query("SELECT * FROM competition
    INNER JOIN competition_seasons ON competition.id = competition_seasons.competition_id
    WHERE country_id IS NOT NULL");
    $sql->execute();
    $comps = $sql->fetchAll();
    return $response->withJson($comps);
});

this will return the following response:

[
  {
    "id": "1",
    "country_id": "1",
    "name": "2017",
    "link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
    "competition_id": "1093",
    "season_id": "13198",
    "update_at": "2018-05-14 12:51:17"
  },
  {
    "id": "2",
    "country_id": "1",
    "name": "2018",
    "link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
    "competition_id": "1093",
    "season_id": "14963",
    "update_at": "2018-05-16 12:10:16"
  },
  {
    "id": "5",
    "country_id": "1",
    "name": "2016",
    "link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
    "competition_id": "1093",
    "season_id": "12091",
    "update_at": "2018-05-14 12:52:47"
  },
  {
    "id": "6",
    "country_id": "1",
    "name": "2015",
    "link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
    "competition_id": "1093",
    "season_id": "10891",
    "update_at": "2018-05-14 12:54:12"
  },
  {
    "id": "7",
    "country_id": "1",
    "name": "2014",
    "link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
    "competition_id": "1093",
    "season_id": "8868",
    "update_at": "2018-05-14 12:55:40"
  },
  {
    "id": "8",
    "country_id": "1",
    "name": "2013",
    "link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
    "competition_id": "1093",
    "season_id": "7851",
    "update_at": "2018-05-14 12:57:02"
  },
  {
    "id": "9",
    "country_id": "1",
    "name": "2012",
    "link": "https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
    "competition_id": "1093",
    "season_id": "7816",
    "update_at": "2018-05-14 12:58:34"
  },
  {
    "id": "10",
    "country_id": "2",
    "name": "2017\/2018",
    "link": "https:\/\/int.soccerway.com\/national\/albania\/super-league\/c48\/",
    "competition_id": "48",
    "season_id": "14214",
    "update_at": "2018-05-16 12:11:12"
  }
]

as you can see the competition available have the id 1093 and different seasons, how can I optimize the query (if is possible), to return a response which contains one competition with a list of seasons?

Something like:

[
    {
        "id":"1",
        "country_id":"1",
        "name":"2017",
        "link":"https:\/\/int.soccerway.com\/national\/afghanistan\/afghan-premier-league\/c1093\/",
        "competition_id":"1093",
        "seasons":[
            "season_id":13198,
            "season_id":12091, 
            etc..
        ],
        "update_at":"2018-05-14 12:51:17"
    }
]

Thanks.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥200 uniapp长期运行卡死问题解决
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
    • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
    • ¥15 乘性高斯噪声在深度学习网络中的应用