dongzhang6677 2014-07-29 14:54
浏览 17

Json中的数组与Symfony2

this is what i want:

[{
    "id": 1,
    "targetGroup": {
        "age": [5],
        "gender": [1],
        "income": [2]
    }
    ...
    ,
    {
        ...
    }
}]

i want an array in my json return.

Here is my Symfony Action:

public function advertisingPartnersAction() {
    $serializer = SerializerBuilder::create()->build();

    $em = $this->getDoctrine()->getRepository('MainBundle:Promotion');
    $query = $em->createQueryBuilder('qbPromotion')
            ->select('qbPromotion.id,qbEntry.entTitle,qbIndustry.indTitle,qbPacking.packTitle,qbEntry.edition,qbEntry.tkp')
            ->join('qbPromotion.entry2', 'qbEntry')
            ->join('qbEntry.packing', 'qbPacking')
            ->join('qbEntry.industry', 'qbIndustry')
            ->getQuery();

    $entity = $query->getResult();

    if ($entity) {
        $jsonEntity = $serializer->serialize($entity, 'json');
    } else {
        // TODO error
        die;
    }

    return new JsonResponse(json_decode($jsonEntity, true));
}

This works fine and returns:

[
    {"id":2,"entTitle":"Titel","indTitle":"Food","packTitle":"Karton","edition":1,"tkp":"38.10"},
    {"id":3,"entTitle":"Titel","indTitle":"Food","packTitle":"Karton","edition":1,"tkp":"38.10"},
    {"id":1,"entTitle":"Titel 2","indTitle":"Getr\u00e4nke","packTitle":"Flasche","edition":2,"tkp":"38.20"}
]

But i need an array like targetGroup in my json. How can i get it work?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
    • ¥15 相敏解调 matlab
    • ¥15 求lingo代码和思路
    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法
    • ¥15 如何绘制动力学系统的相图
    • ¥15 对接wps接口实现获取元数据
    • ¥20 给自己本科IT专业毕业的妹m找个实习工作