在Symfony中反序列化对象

时间:2017-02-14 11:30:30

标签: json symfony

我在$ Data

中检索了mailjet API的json响应
class DefaultController extends FOSRestController                                   {                                                                                    
/**                                                                              
 * @Rest\View()                                                                  
 * @Rest\Get("/apitest")                                                         
 */                                                                      

   public function indexAction()                                                    
    {                                                                                
        $apikey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxx';                                
        $apisecret = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';                             
        $mj = new \Mailjet\Client($apikey, $apisecret);                              
        $id=17361101;                                                                
        $Data = $mj->get(Resources::$Message, ['id' => $id]);                      
        $Data= $Data->getData();                                                     
        return $Data;} 

The Response Json:

[{"ArrivedAt":"2016-05-13T07:34:47Z","AttachmentCount":0,"AttemptCount":0,"CampaignID":4884268551,"ContactID":1690868613,"Delay":0,"DestinationID":12344543,"FilterTime":61,"ID":17361101,"IsClickTracked":true,"IsHTMLPartIncluded":true,"IsOpenTracked":true,"IsTextPartIncluded":false,"IsUnsubTracked":false,"MessageSize":19308,"SenderID":4294850021,"SpamassassinScore":0,"SpamassRules":"","StatePermanent":false,"Status":"clicked"}]

我创建了一个包含Api响应的所有属性的实体消息

<?php 
namespace DashbordMailjetBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 */
 class Message
 {

/**
 * @ORM\Column(type="datetime" ,name="ArrivedAt")
 */
private $ArrivedAt;
/**
 * @ORM\Column(type="bigint",name="AttachmentCount")
 */
private $AttachmentCount;
/**
 * @ORM\Column(type="bigint",name="AttemptCount")
 */
private $AttemptCount;
/**
 * @ORM\Column(type="bigint",name="CampaignID")
 */
private $CampaignID;
/**
 * @ORM\Column(type="bigint",name="ContactID")
 */
private $ContactID;
/**
 * @ORM\Column(type="bigint",name="Delay")
 */
private $Delay;
/**
 * @ORM\Column(type="bigint",name="DestinationID")
 */
private $DestinationID;
/**
 * @ORM\Column(type="bigint",name="FilterTime")
 */
private $FilterTime;
/**
 * @var bigint $uid
 *
 * @ORM\Column(name="id", type="bigint", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $ID;
/**
 * @ORM\Column(type="boolean",name="IsClickTracked")
 */
private $IsClickTracked;
/**
 * @ORM\Column(type="boolean",name="IsHTMLPartIncluded")
 */
private $IsHTMLPartIncluded;
/**
 * @ORM\Column(type="boolean",name="IsOpenTracked")
 */
private $IsOpenTracked;
/**
 * @var boolean
 *
 * @ORM\Column(type="boolean",name="IsTextPartIncluded")
 */
private $IsTextPartIncluded;
/**
 * @ORM\Column(type="boolean",name="IsUnsubTracked")
 */
private $IsUnsubTracked;
/**
 * @ORM\Column(type="bigint",name="MessageSize")
 */
private $MessageSize;
/**
 * @ORM\Column(type="bigint",name="SenderID")
 */
private $SenderID;

/**
 * @ORM\Column(type="string",name="SpamassassinScore")
 */
private $SpamassassinScore;
/**
 * @ORM\Column(type="string",name="SpamassRules", nullable=true)
 */
private $SpamassRules;
/**
 * @ORM\Column(type="boolean",name="StatePermanent")
 */
private $StatePermanent;
/**
 * @ORM\Column(type="string",name="Status")
 */
private $Status;

/**
 * @return datetime
 */
public function getArrivedAt()
{
    return $this->ArrivedAt;
}

/**
 * @param datetime $ArrivedAt
 */
public function setArrivedAt($ArrivedAt)
{
    $this->ArrivedAt = $ArrivedAt;
}

/**
 * @return mixed
 */
public function getAttachmentCount()
{
    return $this->AttachmentCount;
}

/**
 * @param mixed $AttachmentCount
 */
public function setAttachmentCount($AttachmentCount)
{
    $this->AttachmentCount = $AttachmentCount;
}

/**
 * @return int
 */
public function getAttemptCount()
{
    return $this->AttemptCount;
}

/**
 * @param int $AttemptCount
 */
public function setAttemptCount($AttemptCount)
{
    $this->AttemptCount = $AttemptCount;
}

/**
 * @return int
 */
public function getCampaignID()
{
    return $this->CampaignID;
}

/**
 * @param int $CampaignID
 */
public function setCampaignID($CampaignID)
{
    $this->CampaignID = $CampaignID;
}

/**
 * @return int
 */
public function getContactID()
{
    return $this->ContactID;
}

/**
 * @param int $ContactID
 */
public function setContactID($ContactID)
{
    $this->ContactID = $ContactID;
}

/**
 * @return int
 */
public function getDelay()
{
    return $this->Delay;
}

/**
 * @param int $Delay
 */
public function setDelay($Delay)
{
    $this->Delay = $Delay;
}

/**
 * @return int
 */
public function getDestinationID()
{
    return $this->DestinationID;
}

/**
 * @param int $DestinationID
 */
public function setDestinationID($DestinationID)
{
    $this->DestinationID = $DestinationID;
}

/**
 * @return int
 */
public function getFilterTime()
{
    return $this->FilterTime;
}

/**
 * @param int $FilterTime
 */
public function setFilterTime($FilterTime)
{
    $this->FilterTime = $FilterTime;
}

/**
 * @return mixed
 */
public function getID()
{
    return $this->ID;
}

/**
 * @param mixed $ID
 */
public function setID($ID)
{
    $this->ID = $ID;
}

/**
 * @return boolean
 */
public function isIsClickTracked()
{
    return $this->IsClickTracked;
}

/**
 * @param boolean $IsClickTracked
 */
public function setIsClickTracked($IsClickTracked)
{
    $this->IsClickTracked = $IsClickTracked;
}

/**
 * @return boolean
 */
public function isIsHTMLPartIncluded()
{
    return $this->IsHTMLPartIncluded;
}

/**
 * @param boolean $IsHTMLPartIncluded
 */
public function setIsHTMLPartIncluded($IsHTMLPartIncluded)
{
    $this->IsHTMLPartIncluded = $IsHTMLPartIncluded;
}

/**
 * @return boolean
 */
public function isIsOpenTracked()
{
    return $this->IsOpenTracked;
}

/**
 * @param boolean $IsOpenTracked
 */
public function setIsOpenTracked($IsOpenTracked)
{
    $this->IsOpenTracked = $IsOpenTracked;
}

/**
 * @return boolean
 */
public function isIsTextPartIncluded()
{
    return $this->IsTextPartIncluded;
}

/**
 * @param boolean $IsTextPartIncluded
 */
public function setIsTextPartIncluded($IsTextPartIncluded)
{
    $this->IsTextPartIncluded = $IsTextPartIncluded;
}

/**
 * @return boolean
 */
public function isIsUnsubTracked()
{
    return $this->IsUnsubTracked;
}

/**
 * @param boolean $IsUnsubTracked
 */
public function setIsUnsubTracked($IsUnsubTracked)
{
    $this->IsUnsubTracked = $IsUnsubTracked;
}

/**
 * @return int
 */
public function getMessageSize()
{
    return $this->MessageSize;
}

/**
 * @param int $MessageSize
 */
public function setMessageSize($MessageSize)
{
    $this->MessageSize = $MessageSize;
}

/**
 * @return int
 */
public function getSenderID()
{
    return $this->SenderID;
}

/**
 * @param int $SenderID
 */
public function setSenderID($SenderID)
{
    $this->SenderID = $SenderID;
}

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

/**
 * @param string $SpamassassinScore
 */
public function setSpamassassinScore($SpamassassinScore)
{
    $this->SpamassassinScore = $SpamassassinScore;
}

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

/**
 * @param string $SpamassRules
 */
public function setSpamassRules($SpamassRules)
{
    $this->SpamassRules = $SpamassRules;
}

/**
 * @return boolean
 */
public function isStatePermanent()
{
    return $this->StatePermanent;
}

/**
 * @param boolean $StatePermanent
 */
public function setStatePermanent($StatePermanent)
{
    $this->StatePermanent = $StatePermanent;
}

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

/**
 * @param string $Status
 */
public function setStatus($Status)
{
    $this->Status = $Status;
}}

我正在尝试反序列化消息对象中的api响应

<?php                                                             

namespace DashbordMailjetBundle\Controller;                       
use DashbordMailjetBundle\Entity\Message;                         
use FOS\RestBundle\Controller\Annotations as Rest;                
use FOS\RestBundle\Controller\FOSRestController;                  
use \Mailjet\Resources;                                                
use JMS\Serializer\SerializerBuilder;                             

class DefaultController extends FOSRestController                 
{                                                                 
 /**                                                           
 * @Rest\View()                                               
 * @Rest\Get("/apitest")                                      
 */                                                           
public function indexAction()                                 
{                                                             
 $apikey = 'xxxxxxxxxxxxxxxxxxxx';                                        
 $apisecret = 'xxxxxxxxxxxxxxxxxxxxx';                                     
 $mj = new \Mailjet\Client($apikey, $apisecret);                                      
 $id=17361101;                                                                        
 $Data = $mj->get(Resources::$Message, ['id' => $id]);                                
 $Data= $Data->getData();                                                             
 $serializer = SerializerBuilder::create()->build();                                  
 $msg=$serializer->deserialize($Data, 'DashbordMailjetBundle\Entity  \Message', 'json');
 $em = $this->getDoctrine()->getManager();                                            
 $em->persist($msg);                                                                  
 return $msg;                                                                         
 }                     

但我有这个错误:Warning: json_decode() expects parameter 1 to be string, array given,有什么帮助吗?

{"error":{"code":500,"message":"Internal Server Error","exception":         [{"message":"Warning: json_decode() expects parameter 1 to be string, array given","class":"Symfony\\Component\\Debug\\Exception\\ContextErrorException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"\/home\/chayma\/my_project_name\/vendor\/jms\/serializer\/src\/JMS\/Serializer\/JsonDeserializationVisitor.php","line":27,"args":[]},{"namespace":"Symfony\\Component\\Debug","short_class":"ErrorHandler","class":"Symfony\\Component\\Debug\\ErrorHandler","type":"->","function":"handleError","file":null,"line":null,"args":[["string","2"],["string","json_decode() expects parameter 1 to be string, array given"],["string","\/home\/chayma\/my_project_name\/vendor\/jms\/serializer\/src\/JMS\/Serializer\/JsonDeserializationVisitor.php"],["string","27"],["array",{"str":["array",[["array",{"ArrivedAt":["string","2016-05-13T07:34:47Z"],"AttachmentCount":["string","0"],"AttemptCount":["string","0"],"CampaignID":["string","4884268551"],"ContactID":["string","1690868613"],"Delay":["string","0"],"DestinationID":["string","12344543"],"FilterTime":["string","61"],"ID":["string","17361101"],"IsClickTracked":["boolean",true],"IsHTMLPartIncluded":["boolean",true],"IsOpenTracked":["boolean",true],"IsTextPartIncluded":["boolean",false],"IsUnsubTracked":["boolean",false],"MessageSize":["string","19308"],"SenderID":["string","4294850021"],"SpamassassinScore":["string","0"],"SpamassRules":["string",""],"StatePermanent":["boolean",false],"Status":["string","clicked"]}]]]}]]},

2 个答案:

答案 0 :(得分:1)

如前所述,序列化程序需要一个字符串,但是,如果我打印$Data->getData()的结果,我会得到一个数组,如下所示:

Array
(
    [0] => Array
        (
            [ArrivedAt] => 2017-02-15T10:01:00Z
            [AttachmentCount] => 0
            [AttemptCount] => 0
            [CampaignID] => 5946xxxxxx
            [ContactID] => 1725xxxxxx
            [Delay] => 0
            [DestinationID] => 14
            [FilterTime] => 111
            [ID] => 16888626888xxxxxx
            [IsClickTracked] =>
            [IsHTMLPartIncluded] => 1
            [IsOpenTracked] => 1
            [IsTextPartIncluded] => 1
            [IsUnsubTracked] =>
            [MessageSize] => 2213
            [SenderID] => 4863xxxxxx
            [SpamassassinScore] => 0
            [SpamassRules] =>
            [StatePermanent] =>
            [Status] => opened
        )

)

我建议您查看PHP wrapperdocumentation for PHP users的存储库。

hAPI编码!

免责声明:Mailjet的员工在这里。

答案 1 :(得分:0)

根据serializer documentation的定义,$serializer->deserialize()需要一个字符串。

您的$Data->getData();似乎返回了一个数组。 (这就是错误信息告诉你的; D)