2012-03-25 183 views

回答

65

這應該做的伎倆!

// convert object => json 
$json = json_encode($myObject); 

// convert json => object 
$obj = json_decode($json); 

下面是一個例子

$foo = new StdClass(); 
$foo->hello = "world"; 
$foo->bar = "baz"; 

$json = json_encode($foo); 
echo $json; 
//=> {"hello":"world","bar":"baz"} 

print_r(json_decode($json)); 
// stdClass Object 
// (
// [hello] => world 
// [bar] => baz 
//) 

如果你想輸出作爲數組,而不是一個對象,通過truejson_decode

print_r(json_decode($json, true)); 
// Array 
// (
// [hello] => world 
// [bar] => baz 
//)  

更多json_encode()

參見:json_decode()

+2

一個我處理現在的問題 - 使用json_decode(),我得到一個stdClass的對象,不是我原來的對象。我可以使用序列化/反序列化,但是我的對象會隨着時間的推移而改變結構,即使有點小,渲染序列化不可用。 – Dennis 2015-03-25 20:41:03

+1

@Dennis'unserialize'將總是返回一個'stdClass'的實例,這不會使它無法使用。你可以很容易地設計你的API來支持'$ attrs = unserialize($ json); $ person = new Person($ attrs);''Person'構造函數可以相應地分配屬性。 – 2015-03-27 00:42:30

3
json_decode($json, true); 
// the second param being true will return associative array. This one is easy. 
12

對於大規模應用程序的更多擴展性使用oop樣式與封裝字段。

簡單的方法: -

class Fruit implements JsonSerializable { 

     private $type = 'Apple', $lastEaten = null; 

     public function __construct() { 
      $this->lastEaten = new DateTime(); 
     } 

     public function jsonSerialize() { 
      return [ 
       'category' => $this->type, 
       'EatenTime' => $this->lastEaten->format(DateTime::ISO8601) 
      ]; 
     } 
    } 

回聲json_encode(新果()); //它輸出:

{"category":"Apple","EatenTime":"2013-01-31T11:17:07-0500"} 

房地產GSON對PHP: -

  1. http://jmsyst.com/libs/serializer
  2. http://symfony.com/doc/current/components/serializer.html
  3. http://framework.zend.com/manual/current/en/modules/zend.serializer.html
  4. http://fractal.thephpleague.com/ - 僅序列
+0

任何正在執行的函數jsonSerialize的反轉 – 2016-08-29 15:37:11

+0

與使用json問題是相反的部分,因爲jeson_decode只返回一個stdClass對象。您需要能夠通過提供一組字段值來初始化您的類的對象。開銷太多了。只是使用普通的序列化。 – Hafenkranich 2016-11-15 12:32:27

0

我ま一種解決這個問題的方法。 我的做法是:

1 - 創建一個抽象類,該類有一個使用Regex將對象轉換爲數組(包括私有屬性)的方法。 2 - 將返回的數組轉換爲json。

我使用這個抽象類作爲我所有的領域類的父

類代碼:

namespace Project\core; 

abstract class AbstractEntity { 
    public function getAvoidedFields() { 
     return array(); 
    } 
    public function toArray() { 
     $temp = (array) $this; 

     $array = array(); 

     foreach ($temp as $k => $v) { 
      $k = preg_match ('/^\x00(?:.*?)\x00(.+)/', $k, $matches) ? $matches [1] : $k; 
      if (in_array ($k, $this->getAvoidedFields())) { 
       $array [$k] = ""; 
      } else { 

       // if it is an object recursive call 
       if (is_object ($v) && $v instanceof AbstractEntity) { 
        $array [$k] = $v->toArray(); 
       } 
       // if its an array pass por each item 
       if (is_array ($v)) { 

        foreach ($v as $key => $value) { 
         if (is_object ($value) && $value instanceof AbstractEntity) { 
          $arrayReturn [$key] = $value->toArray(); 
         } else { 
          $arrayReturn [$key] = $value; 
         } 
        } 
        $array [$k] = $arrayReturn; 
       } 
       // if it is not a array and a object return it 
       if (! is_object ($v) && !is_array ($v)) { 
        $array [$k] = $v; 
       } 
      } 
     } 

     return $array; 
    } 
}