将值分配给数组然后在 symfony 中将其编码为 json 的最佳方法或实践
Best way or practice to assign values to array and then encode it to json in symfony
所以目前我有这个代码:
//getting products
$productsFirst = $this->productRepository->findBy(
['weather' => $conditionFirst],
['price' => 'ASC'],
2
);
$productsSecond = $this->productRepository->findBy(
['weather' => $conditionSecond],
['price' => 'ASC'],
2
);
$productsThird = $this->productRepository->findBy(
['weather' => $conditionThird],
['price' => 'ASC'],
2
);
//serializing products
$normalizers = [
new ObjectNormalizer(),
];
$encoders = [
new JsonEncoder(),
];
$serializer = new Serializer($normalizers, $encoders);
$productsFirst = $serializer->serialize($productsFirst, format:'json',);
$productsSecond = $serializer->serialize($productsSecond, format:'json',);
$productsThird = $serializer->serialize($productsThird, format:'json',);
$arr = array(
$city => array(
"weather_forecast" => $conditionFirst,
"date" => $dateFirst,
"product" => $productsFirst
),
array(
"weather_forecast" => $conditionSecond,
"date" => $dateSecond,
"product" => $productsSecond
),
array(
"weather_forecast" => $conditionThird,
"date" => $dateThird,
"product" => $productsThird
)
);
return $arr = json_encode($arr, | JSON_PRETTY_PRINT);
我收到了这个 json 回复:
{
"London": {
"weather_forecast": "clear",
"date": "2021-06-08",
"product": "[{\"id\":903,\"sku\":\"6456909228\",\"name\":\"Abdul Bergstrom\",\"price\":0.45,\"weather\":\"clear\"},{\"id\":1074,\"sku\":\"6540442971\",\"name\":\"Gladys Kirlin\",\"price\":6.73,\"weather\":\"clear\"}]"
},
"0": {
"weather_forecast": "clear",
"date": "2021-06-09",
"product": "[{\"id\":903,\"sku\":\"6456909228\",\"name\":\"Abdul Bergstrom\",\"price\":0.45,\"weather\":\"clear\"},{\"id\":1074,\"sku\":\"6540442971\",\"name\":\"Gladys Kirlin\",\"price\":6.73,\"weather\":\"clear\"}]"
},
"1": {
"weather_forecast": "light-rain",
"date": "2021-06-10",
"product": "[{\"id\":1103,\"sku\":\"8486705088\",\"name\":\"Dr. Aniyah Stehr IV\",\"price\":1.62,\"weather\":\"light-rain\"},{\"id\":931,\"sku\":\"3629804527\",\"name\":\"Jean Kirlin\",\"price\":5.11,\"weather\":\"light-rain\"}]"
}
}
但我想从 json 中删除 id 和天气。解码然后再次编码似乎是一种糟糕的方法。我可以在将它们序列化为 json 之前访问所需的值吗?我想要这样的东西:
{
"London": {
"weather_forecast": "clear",
"date": "2021-06-09",
"product": [
{
"name": "Dr. Aniyah Stehr IV",
"sku": "8486705088",
"price": "94.68"
},
{
"name": "Jean Kirlin",
"sku": "3629804527",
"price": "10.76"
}
},
"0": {
"weather_forecast": "clear",
"date": "2021-06-09",
"product": [
{
"name": "Dr. Aniyah Stehr IV",
"sku": "8486705088",
"price": "94.68"
},
{
"name": "Jean Kirlin",
"sku": "3629804527",
"price": "10.76"
}
抱歉,如果这是一个愚蠢的问题,我仍然了解 symfony。
您不需要序列化您的实体数组。
您可以在您的实体中声明
use JsonSerializable;
/**
* @Entity(repositoryClass="App\Entity\MyEntity::class")
* @Table(name="user")
*/
class MyEntity implements JsonSerializable
{
/** @Column(length=50) */
private $name;
/** @Column(length=50) */
private $sku;
/** @Column(length=50) */
private $price;
public function jsonSerialize()
{
return array(
'name' => $this->name,
'sku'=> $this->sku,
'price'=> $this->price,
);
}
}
然后调用你的代码,你可以直接在这些实体上调用json_encode:
//getting products
$productsFirst = $this->productRepository->findBy(
['weather' => $conditionFirst],
['price' => 'ASC'],
2
);
$productsSecond = $this->productRepository->findBy(
['weather' => $conditionSecond],
['price' => 'ASC'],
2
);
$productsThird = $this->productRepository->findBy(
['weather' => $conditionThird],
['price' => 'ASC'],
2
);
$arr = array(
$city => array(
"weather_forecast" => $conditionFirst,
"date" => $dateFirst,
"product" => $productsFirst
),
array(
"weather_forecast" => $conditionSecond,
"date" => $dateSecond,
"product" => $productsSecond
),
array(
"weather_forecast" => $conditionThird,
"date" => $dateThird,
"product" => $productsThird
)
);
return $arr = json_encode($arr, | JSON_PRETTY_PRINT);
所以目前我有这个代码:
//getting products
$productsFirst = $this->productRepository->findBy(
['weather' => $conditionFirst],
['price' => 'ASC'],
2
);
$productsSecond = $this->productRepository->findBy(
['weather' => $conditionSecond],
['price' => 'ASC'],
2
);
$productsThird = $this->productRepository->findBy(
['weather' => $conditionThird],
['price' => 'ASC'],
2
);
//serializing products
$normalizers = [
new ObjectNormalizer(),
];
$encoders = [
new JsonEncoder(),
];
$serializer = new Serializer($normalizers, $encoders);
$productsFirst = $serializer->serialize($productsFirst, format:'json',);
$productsSecond = $serializer->serialize($productsSecond, format:'json',);
$productsThird = $serializer->serialize($productsThird, format:'json',);
$arr = array(
$city => array(
"weather_forecast" => $conditionFirst,
"date" => $dateFirst,
"product" => $productsFirst
),
array(
"weather_forecast" => $conditionSecond,
"date" => $dateSecond,
"product" => $productsSecond
),
array(
"weather_forecast" => $conditionThird,
"date" => $dateThird,
"product" => $productsThird
)
);
return $arr = json_encode($arr, | JSON_PRETTY_PRINT);
我收到了这个 json 回复:
{
"London": {
"weather_forecast": "clear",
"date": "2021-06-08",
"product": "[{\"id\":903,\"sku\":\"6456909228\",\"name\":\"Abdul Bergstrom\",\"price\":0.45,\"weather\":\"clear\"},{\"id\":1074,\"sku\":\"6540442971\",\"name\":\"Gladys Kirlin\",\"price\":6.73,\"weather\":\"clear\"}]"
},
"0": {
"weather_forecast": "clear",
"date": "2021-06-09",
"product": "[{\"id\":903,\"sku\":\"6456909228\",\"name\":\"Abdul Bergstrom\",\"price\":0.45,\"weather\":\"clear\"},{\"id\":1074,\"sku\":\"6540442971\",\"name\":\"Gladys Kirlin\",\"price\":6.73,\"weather\":\"clear\"}]"
},
"1": {
"weather_forecast": "light-rain",
"date": "2021-06-10",
"product": "[{\"id\":1103,\"sku\":\"8486705088\",\"name\":\"Dr. Aniyah Stehr IV\",\"price\":1.62,\"weather\":\"light-rain\"},{\"id\":931,\"sku\":\"3629804527\",\"name\":\"Jean Kirlin\",\"price\":5.11,\"weather\":\"light-rain\"}]"
}
}
但我想从 json 中删除 id 和天气。解码然后再次编码似乎是一种糟糕的方法。我可以在将它们序列化为 json 之前访问所需的值吗?我想要这样的东西:
{
"London": {
"weather_forecast": "clear",
"date": "2021-06-09",
"product": [
{
"name": "Dr. Aniyah Stehr IV",
"sku": "8486705088",
"price": "94.68"
},
{
"name": "Jean Kirlin",
"sku": "3629804527",
"price": "10.76"
}
},
"0": {
"weather_forecast": "clear",
"date": "2021-06-09",
"product": [
{
"name": "Dr. Aniyah Stehr IV",
"sku": "8486705088",
"price": "94.68"
},
{
"name": "Jean Kirlin",
"sku": "3629804527",
"price": "10.76"
}
抱歉,如果这是一个愚蠢的问题,我仍然了解 symfony。
您不需要序列化您的实体数组。
您可以在您的实体中声明
use JsonSerializable;
/**
* @Entity(repositoryClass="App\Entity\MyEntity::class")
* @Table(name="user")
*/
class MyEntity implements JsonSerializable
{
/** @Column(length=50) */
private $name;
/** @Column(length=50) */
private $sku;
/** @Column(length=50) */
private $price;
public function jsonSerialize()
{
return array(
'name' => $this->name,
'sku'=> $this->sku,
'price'=> $this->price,
);
}
}
然后调用你的代码,你可以直接在这些实体上调用json_encode:
//getting products
$productsFirst = $this->productRepository->findBy(
['weather' => $conditionFirst],
['price' => 'ASC'],
2
);
$productsSecond = $this->productRepository->findBy(
['weather' => $conditionSecond],
['price' => 'ASC'],
2
);
$productsThird = $this->productRepository->findBy(
['weather' => $conditionThird],
['price' => 'ASC'],
2
);
$arr = array(
$city => array(
"weather_forecast" => $conditionFirst,
"date" => $dateFirst,
"product" => $productsFirst
),
array(
"weather_forecast" => $conditionSecond,
"date" => $dateSecond,
"product" => $productsSecond
),
array(
"weather_forecast" => $conditionThird,
"date" => $dateThird,
"product" => $productsThird
)
);
return $arr = json_encode($arr, | JSON_PRETTY_PRINT);