2016-08-15 53 views
-3

需要你的幫助。Deserialise json對象主義實體

我json對象,看起來像:

{ 
    "succes": true, 
    "count": 6, 
    "result": { 
    "main": [ 
     { 
     "id": 21, 
     "name": "\u0421\u043f\u0443\u0442\u043d\u0438\u043a", 
     "url": "\/cinema\", 
     "image": "\/upload\/kinoteatrs\/21\/1340272755sputnik-.jpg", 
     "vote": "8,2", 
     "count_vote": "1479 \u0433\u043e\u043b\u043e\u0441\u043e\u0432", 
     "phone": "(044) 243-46-32; (044) 243-46-33 (\u0430\u0432\u0442\u043e\u043e\u0442\u0432.)", 
     "address": "\u0433. \u041a\u0438\u0435\u0432, \u0443\u043b. \u0418\u0441\u043a\u0440\u043e\u0432\u0441\u043a\u0430\u044f, 18" 
     }, 
     { 
     "id": 126, 
     "name": "\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441 \u0432 \u0422\u0420\u0426 \"Sky Mall\"", 
     "url": "\/cinema\", 
     "image": "\/upload\/kinoteatrs\/126\/1429607129megapleks.png", 
     "vote": "7,4", 
     "count_vote": "829 \u0433\u043e\u043b\u043e\u0441\u043e\u0432", 
     "phone": "(096)129 48 54, (044) 290 83 75", 
     "address": "\u0433. \u041a\u0438\u0435\u0432, \u043f\u0440-\u0442 \u0413\u0435\u043d\u0435\u0440\u0430\u043b\u0430 \u0412\u0430\u0442\u0443\u0442\u0438\u043d\u0430, 2-\u0422" 
     }, 
     { 
     "id": 108, 
     "name": "\u041c\u0443\u043b\u044c\u0442\u0438\u043f\u043b\u0435\u043a\u0441 \u0432 \u0422\u0420\u0426 \"\u041a\u043e\u043c\u043e\u0434\"", 
     "url": "\/cinema\", 
     "image": "\/upload\/kinoteatrs\/108\/1429607154multipleks-v-komode.png", 
     "vote": "7,1", 
     "count_vote": "409 \u0433\u043e\u043b\u043e\u0441\u043e\u0432", 
     "phone": "(044)-593-35-77, 593-35-80", 
     "address": "\u0433. \u041a\u0438\u0435\u0432, \u0443\u043b. \u041b\u0443\u043d\u0430\u0447\u0430\u0440\u0441\u043a\u043e\u0433\u043e 4, 4-\u044d\u0442\u0430\u0436" 
     } 
    ], 
    "unmain": [ 
     { 
     "id": 14, 
     "name": "\u041b\u0435\u0439\u043f\u0446\u0438\u0433", 
     "url": "\/cinema\", 
     "image": "\/upload\/kinoteatrs\/14\/1332685506lejpcig.png", 
     "vote": "8,0", 
     "count_vote": "3402 \u0433\u043e\u043b\u043e\u0441\u0430", 
     "phone": "407-19-93, 403-86-59, 403-86-60", 
     "address": "\u0433. \u041a\u0438\u0435\u0432, \u043f\u0440\u043e\u0441\u043f. \u041b\u0435\u0441\u044f \u041a\u0443\u0440\u0431\u0430\u0441\u0430, 8" 
     }, 
     { 
     "id": 13, 
     "name": "\u0424\u043b\u043e\u0440\u0435\u043d\u0446\u0438\u044f", 
     "url": "\/cinema\", 
     "image": "\/upload\/kinoteatrs\/13\/1332686274florencija.png", 
     "vote": "8,0", 
     "count_vote": "2134 \u0433\u043e\u043b\u043e\u0441\u0430", 
     "phone": "(044) 515 87 81", 
     "address": "\u0433.\u041a\u0438\u0435\u0432, \u043f\u0440\u043e\u0441\u043f. \u041c\u0430\u044f\u043a\u043e\u0432\u0441\u043a\u043e\u0433\u043e, 31" 
     }, 
     { 
     "id": 134, 
     "name": "\u0411\u0430\u0442\u0442\u0435\u0440\u0444\u043b\u044f\u0439 \u041a\u043e\u0441\u043c\u043e\u043f\u043e\u043b\u0438\u0442", 
     "url": "\/cinema\", 
     "image": "\/upload\/kinoteatrs\/134\/1332684447bolshevik.jpg", 
     "vote": "7,6", 
     "count_vote": "1512 \u0433\u043e\u043b\u043e\u0441\u043e\u0432", 
     "phone": "200-90-18, 200-90-20; 200-90-10, 200-90-12 (\u0430\u0432\u0442\u043e\u043e\u0442\u0432.)", 
     "address": "\u0433. \u041a\u0438\u0435\u0432, \u0443\u043b. \u0413\u0435\u0442\u044c\u043c\u0430\u043d\u0430, 6" 
     } 
    ] 
    } 
} 

而且我有實體

<?php 
namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* 
* @ORM\Entity 
* @ORM\Table(name="cinemas") 
*/ 
class Cinemas 
{ 
    /** 
    * @ORM\Column(type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $name; 
    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $phone; 
    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $address; 
    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $url; 
    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $image; 
    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $vote; 
    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    private $countVote; 


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


} 

的追問是:什麼是deserialise此JSON的最佳途徑,堅持內主要的所有對象並且對數據庫不起作用。 需要最佳實踐,謝謝!

更新:呃,實際上,我想我用錯誤的方式描述了我的建議..我知道如何從這個json獲得std類的實例。例如,我們有:

[0] => stdClass Object 
      (
       [id] => 21 
       [name] => text 
       [url] => /cinema/sputnik- 
       [image] => /upload/kinoteatrs/21/1340272755sputnik-.jpg 
       [vote] => 8,2 
       [count_vote] => 1479 votes 
       [phone] => (014) 223-46-32; (054) 245-46-33 
       [address] => address 
      ) 

該任務是如何將此std類轉換爲Cinema實體。

我覺得應該有$serializer->deserialize();

可以使是的,我可以編碼這個對象再次JSON,然後做類似

$serializer->deserialize($data, 'Acme\Cinema[]', 'json'); 

,或者我們可以通過循環設置每個屬性

但它看起來不像解決方案..

+0

最佳實踐:始終以[在本例中閱讀本手冊爲'json_decode()'](http://php.net/manual/en/function.json-decode.php) – RiggsFolly

+0

繪畫牆的代碼和要求**最佳實踐**並沒有掩蓋你沒有明顯的努力來解決你的問題的事實 – RiggsFolly

+0

@RiggsFolly Okey,我現在談論這個,但是如何解碼從main和unmain部分開始。因爲我需要內部的對象來將它們直接保存到Doctrine Manager中。那你& – Saba

回答

1

添加靜態方法到您的實體:

static public function fromJson($data){ 
    $s = new self(); 
    $s->setId($data['id']; 
    //do the same for every other fields 

    return $s 
} 

然後在你的JSON迴路,並使用您剛創建的方法:

$d = json_decode($json); 
foreach($d['result']['main'] as $item){ 
    $c = Cinema::fromJson($item); 
} 

你需要做一些調整,做你想要做的事。

0

那麼我找到解決方案。 假設我們

$cinemas => stdClass Object 
     (
      [id] => 21 
      [name] => text 
      [url] => /cinema/sputnik- 
      [image] => /upload/kinoteatrs/21/1340272755sputnik-.jpg 
      [vote] => 8,2 
      [count_vote] => 1479 votes 
      [phone] => (014) 223-46-32; (054) 245-46-33 
      [address] => address 
     ) 

它轉換爲實體:

 $normalizers = array(new ObjectNormalizer()); 

     $serializer = new Serializer($normalizers); 
     $serializer->denormalize($cinemas, 'AppBundle\Entity\Cinemas'); 

我們獲取的對象是這樣的:

 [0] => AppBundle\Entity\Cinemas Object 
      (
       [id:AppBundle\Entity\Cinemas:private] => 
       [name:AppBundle\Entity\Cinemas:private] => text 
       [phone:AppBundle\Entity\Cinemas:private] => (014) 223-46-32; (054) 245-46-33 
       [address:AppBundle\Entity\Cinemas:private] => text 
       [url:AppBundle\Entity\Cinemas:private] => /cinema/sputnik- 
       [image:AppBundle\Entity\Cinemas:private] => /upload/kinoteatrs/21/1340272755sputnik-.jpg 
       [vote:AppBundle\Entity\Cinemas:private] => 8,2 
       [countVote:AppBundle\Entity\Cinemas:private] => 1479 votes 
      ) 

,然後我們可以使用Doctrine經理堅持這一點。希望爲某人節省時間。