Php 带有Symfony2的Json数组
这就是我想要的:Php 带有Symfony2的Json数组,php,arrays,json,symfony,Php,Arrays,Json,Symfony,这就是我想要的: [{ "id": 1, "targetGroup": { "age": [5], "gender": [1], "income": [2] } ... , { ... } }] 我希望json返回中有一个数组 以下是我的Symfony行动: public function advertisingPartnersAction() { $serializ
[{
"id": 1,
"targetGroup": {
"age": [5],
"gender": [1],
"income": [2]
}
...
,
{
...
}
}]
我希望json返回中有一个数组
以下是我的Symfony行动:
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));
}
这可以很好地工作并返回:
[
{"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"}
]
但我的json中需要一个类似targetGroup的数组。我如何才能让它工作?首先,不要担心如何用JSON格式化。这是为你做的。只需在PHP中创建所需的数据结构。由于您不清楚什么是targetGroup,也不清楚它是如何派生出来的,因此没有很多其他方法可以帮助您。第二,为什么要序列化,然后反序列化,然后重新序列化到JSON/从JSON开始。只要做一次,我想targetGroup是一个条令实体。这就是它被序列化为对象的原因。targetGroup是arrayname,数组有3个表中的3个AttributeId。我需要返回一个包含数组的json。