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 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计