gpt4 book ai didi

php - 将 php symfony 中的多个模型从 json 解码为对象

转载 作者:行者123 更新时间:2023-12-05 01:45:14 24 4
gpt4 key购买 nike

我想将 json 转换为对象/模型。如果 json 只是一维的,它会完美地工作。

但是如果是多维的,只转换外层(用户),不转换内层(公司),这还是一个数组。你能帮我解决这个问题吗?

模型:

<?php
namespace AppBundle;

class Company {
/**
* @var string
*/
protected $companyName = '';

/**
* @return string
*/
public function getCompanyName()
{
return $this->companyName;
}

/**
* @param string $companyName
* @return void
*/
public function setCompanyName($companyName)
{
$this->companyName = $companyName;
}
}

class User {
/**
* @var \AppBundle\Company
*/
protected $company = null;

/**
* @var string
*/
protected $username = '';

/**
* @return \AppBundle\Company
*/
public function getCompany() {
return $this->company;
}

/**
* @param \AppBundle\Company $company
* @return void
*/
public function setCompany($company) {
$this->company = $company;
}

/**
* @return string
*/
public function getUsername() {
return $this->username;
}

/**
* @param string $username
* @return void
*/
public function setUsername($username) {
$this->username = $username;
}
}
?>

将json转化为模型:

<?php
namespace AppBundle\Controller;

class DefaultController extends \Symfony\Bundle\FrameworkBundle\Controller\Controller
{

public function indexAction()
{
// Initialize serializer
$objectNormalizer = new \Symfony\Component\Serializer\Normalizer\ObjectNormalizer();
$jsonEncoder = new \Symfony\Component\Serializer\Encoder\JsonEncoder();
$serializer = new \Symfony\Component\Serializer\Serializer([$objectNormalizer], [$jsonEncoder]);

// Set test model
$company = new \AppBundle\Company();
$company->setCompanyName('MyCompany');
$user = new \AppBundle\User();
$user->setCompany($company);
$user->setUsername('MyUsername');

// Serialize test model to json
$json = $serializer->serialize($user, 'json');
dump($user); // Model ok, Company is instance of \AppBundle\Company
dump($json); // json ok + valide

// Deserialize json to model
$user = $serializer->deserialize($json, \AppBundle\User::class, 'json');
dump($user); // Error: Company is now array instead instance of \AppBundle\Company

// Denormalize json to model
$userArray = $serializer->decode($json, 'json');
$user = $serializer->denormalize($userArray, \AppBundle\User::class);
dump($user); // Error: Company is now array instead instance of \AppBundle\Company
}
}
?>

最佳答案

我解决了这个问题。

一方面,您需要 PHP 7。注释我还没有测试过。

然后必须在 setCompany() 中正确设置变量。

public function setCompany(Company $company) {
$this->company = $company;
}

并且必须使用 ReflectionExtractor()。

use Symfony\Component\Serializer\Normalizer;
use Symfony\Component\PropertyInfo\Extractor;
$objectNormalizer = new ObjectNormalizer(
null,
null,
null,
new ReflectionExtractor()
);

你只需要deserialize(),因为它是decode()denormalize()http://symfony.com/doc/current/components/serializer.html

完整固定代码:

公司类:

class Company {
/**
* @var string
*/
protected $companyName = '';

/**
* @return string
*/
public function getCompanyName() {
return $this->companyName;
}

/**
* @param string $companyName
* @return void
*/
public function setCompanyName($companyName) {
$this->companyName = $companyName;
}
}

用户类:

class User {
/**
* @var \AppBundle\Company
*/
protected $company = null;

/**
* @var string
*/
protected $username = '';

/**
* @return \AppBundle\Company
*/
public function getCompany() {
return $this->company;
}

/**
* @param \AppBundle\Company $company
* @return void
*/
public function setCompany(Company $company) {
$this->company = $company;
}

/**
* @return string
*/
public function getUsername() {
return $this->username;
}

/**
* @param string $username
* @return void
*/
public function setUsername($username) {
$this->username = $username;
}
}
?>

Controller 类:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\Serializer\Normalizer;
use Symfony\Component\PropertyInfo\Extractor;
use Symfony\Component\Serializer;

class DefaultController extends Controller {
public function indexAction() {
$objectNormalizer = new ObjectNormalizer(
null,
null,
null,
new ReflectionExtractor()
);
$jsonEncoder = new JsonEncoder();
$serializer = new Serializer([$objectNormalizer], [$jsonEncoder]);

$company = new \AppBundle\Company();
$company->setCompanyName('MyCompany');

$user = new \AppBundle\User();
$user->setCompany($company);
$user->setUsername('MyUsername');

$json = $serializer->serialize($user, 'json');
dump($user, $json);

$user2 = $serializer->deserialize($json, \AppBundle\User::class, 'json');
dump($user2);
}
}
?>

关于php - 将 php symfony 中的多个模型从 json 解码为对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42729405/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com