2014-12-08 129 views
1

我有一個我試圖轉換爲JSON的10個對象的數組。該數組存儲在名爲$invoices的變量中。以下是var_dump($invoices)的輸出結果,僅限於第一個對象。如何將此PHP數組對象轉換爲json?

我已經試過如下:

$invoices = json_encode($invoices, FALSE); 
$invoices = json_encode($invoices, TRUE); 
$invoices = json_encode($invoices, JSON_UNESCAPED_UNICODE); 

然而,輸出始終是:

string(31) "[{},{},{},{},{},{},{},{},{},{}]" 

這怎麼陣列正確地轉換爲JSON?

注:我正在運行PHP 5.5.9

更新:

原來,這個問題是由於一個事實,即json_encode不會與工作「受保護的成員變量。」如果我無法訪問創建它們的類,有沒有辦法將這些變量聲明爲public?

array(10) { 
    [0]=> 
    object(QuickBooks_IPP_Object_Invoice)#285 (1) { 
    ["_data":protected]=> 
    array(22) { 
     ["Id"]=> 
     array(1) { 
     [0]=> 
     string(6) "{-224}" 
     } 
     ["SyncToken"]=> 
     array(1) { 
     [0]=> 
     string(1) "0" 
     } 
     ["MetaData"]=> 
     array(1) { 
     [0]=> 
     object(QuickBooks_IPP_Object_MetaData)#282 (1) { 
      ["_data":protected]=> 
      array(2) { 
      ["CreateTime"]=> 
      array(1) { 
       [0]=> 
       string(25) "2014-12-07T09:48:47-08:00" 
      } 
      ["LastUpdatedTime"]=> 
      array(1) { 
       [0]=> 
       string(25) "2014-12-07T09:48:47-08:00" 
      } 
      } 
     } 
     } 
     ["CustomField"]=> 
     array(1) { 
     [0]=> 
     object(QuickBooks_IPP_Object_CustomField)#292 (1) { 
      ["_data":protected]=> 
      array(3) { 
      ["DefinitionId"]=> 
      array(1) { 
       [0]=> 
       string(4) "{-1}" 
      } 
      ["Name"]=> 
      array(1) { 
       [0]=> 
       string(6) "Crew #" 
      } 
      ["Type"]=> 
      array(1) { 
       [0]=> 
       string(10) "StringType" 
      } 
      } 
     } 
     } 
     ["DocNumber"]=> 
     array(1) { 
     [0]=> 
     string(4) "1038" 
     } 
     ["TxnDate"]=> 
     array(1) { 
     [0]=> 
     string(10) "2014-12-07" 
     } 
     ["Line"]=> 
     array(2) { 
     [0]=> 
     object(QuickBooks_IPP_Object_Line)#263 (1) { 
      ["_data":protected]=> 
      array(5) { 
      ["Id"]=> 
      array(1) { 
       [0]=> 
       string(4) "{-1}" 
      } 
      ["LineNum"]=> 
      array(1) { 
       [0]=> 
       string(1) "1" 
      } 
      ["Amount"]=> 
      array(1) { 
       [0]=> 
       string(9) "155555.00" 
      } 
      ["DetailType"]=> 
      array(1) { 
       [0]=> 
       string(19) "SalesItemLineDetail" 
      } 
      ["SalesItemLineDetail"]=> 
      array(1) { 
       [0]=> 
       object(QuickBooks_IPP_Object_SalesItemLineDetail)#765 (1) { 
       ["_data":protected]=> 
       array(2) { 
        ["ItemRef"]=> 
        array(1) { 
        [0]=> 
        string(4) "{-3}" 
        } 
        ["TaxCodeRef"]=> 
        array(1) { 
        [0]=> 
        string(6) "{-NON}" 
        } 
       } 
       } 
      } 
      } 
     } 
     [1]=> 
     object(QuickBooks_IPP_Object_Line)#748 (1) { 
      ["_data":protected]=> 
      array(3) { 
      ["Amount"]=> 
      array(1) { 
       [0]=> 
       string(9) "155555.00" 
      } 
      ["DetailType"]=> 
      array(1) { 
       [0]=> 
       string(18) "SubTotalLineDetail" 
      } 
      ["SubTotalLineDetail"]=> 
      array(1) { 
       [0]=> 
       string(0) "" 
      } 
      } 
     } 
     } 
     ["TxnTaxDetail"]=> 
     array(1) { 
     [0]=> 
     object(QuickBooks_IPP_Object_TxnTaxDetail)#287 (1) { 
      ["_data":protected]=> 
      array(1) { 
      ["TotalTax"]=> 
      array(1) { 
       [0]=> 
       string(1) "0" 
      } 
      } 
     } 
     } 
     ["CustomerRef"]=> 
     array(1) { 
     [0]=> 
     string(5) "{-11}" 
     } 
     ["BillAddr"]=> 
     array(1) { 
     [0]=> 
     object(QuickBooks_IPP_Object_BillAddr)#284 (1) { 
      ["_data":protected]=> 
      array(7) { 
      ["Id"]=> 
      array(1) { 
       [0]=> 
       string(5) "{-11}" 
      } 
      ["Line1"]=> 
      array(1) { 
       [0]=> 
       string(13) "1045 Main St." 
      } 
      ["City"]=> 
      array(1) { 
       [0]=> 
       string(13) "Half Moon Bay" 
      } 
      ["CountrySubDivisionCode"]=> 
      array(1) { 
       [0]=> 
       string(2) "CA" 
      } 
      ["PostalCode"]=> 
      array(1) { 
       [0]=> 
       string(5) "94213" 
      } 
      ["Lat"]=> 
      array(1) { 
       [0]=> 
       string(10) "37.4559621" 
      } 
      ["Long"]=> 
      array(1) { 
       [0]=> 
       string(11) "-122.429939" 
      } 
      } 
     } 
     } 
     ["ShipAddr"]=> 
     array(1) { 
     [0]=> 
     object(QuickBooks_IPP_Object_ShipAddr)#814 (1) { 
      ["_data":protected]=> 
      array(7) { 
      ["Id"]=> 
      array(1) { 
       [0]=> 
       string(5) "{-11}" 
      } 
      ["Line1"]=> 
      array(1) { 
       [0]=> 
       string(13) "1045 Main St." 
      } 
      ["City"]=> 
      array(1) { 
       [0]=> 
       string(13) "Half Moon Bay" 
      } 
      ["CountrySubDivisionCode"]=> 
      array(1) { 
       [0]=> 
       string(2) "CA" 
      } 
      ["PostalCode"]=> 
      array(1) { 
       [0]=> 
       string(5) "94213" 
      } 
      ["Lat"]=> 
      array(1) { 
       [0]=> 
       string(10) "37.4559621" 
      } 
      ["Long"]=> 
      array(1) { 
       [0]=> 
       string(11) "-122.429939" 
      } 
      } 
     } 
     } 
     ["DueDate"]=> 
     array(1) { 
     [0]=> 
     string(10) "2015-01-06" 
     } 
     ["TotalAmt"]=> 
     array(1) { 
     [0]=> 
     string(9) "155555.00" 
     } 
     ["ApplyTaxAfterDiscount"]=> 
     array(1) { 
     [0]=> 
     string(5) "false" 
     } 
     ["PrintStatus"]=> 
     array(1) { 
     [0]=> 
     string(11) "NeedToPrint" 
     } 
     ["EmailStatus"]=> 
     array(1) { 
     [0]=> 
     string(6) "NotSet" 
     } 
     ["Balance"]=> 
     array(1) { 
     [0]=> 
     string(9) "155555.00" 
     } 
     ["Deposit"]=> 
     array(1) { 
     [0]=> 
     string(1) "0" 
     } 
     ["AllowIPNPayment"]=> 
     array(1) { 
     [0]=> 
     string(5) "false" 
     } 
     ["AllowOnlinePayment"]=> 
     array(1) { 
     [0]=> 
     string(5) "false" 
     } 
     ["AllowOnlineCreditCardPayment"]=> 
     array(1) { 
     [0]=> 
     string(5) "false" 
     } 
     ["AllowOnlineACHPayment"]=> 
     array(1) { 
     [0]=> 
     string(5) "false" 
     } 
    } 
    } 
    [1]=> 
    object(QuickBooks_IPP_Object_Invoice)#830 (1) { 
    ["_data":protected]=> 
    array(22) { 
    ... 
    ... 
+0

您應該首先聲明您正在嘗試使用QuickBooks API。 Intuit要求您通過OAuth對QuickBooks Online進行身份驗證(只需一次)。 Intuit要求您註冊一個「應用程序」以連接到QuickBooks Online。 – carlodurso 2014-12-08 02:56:59

+0

@carlodurso,是的,我正在使用QBO API。我的應用程序已註冊並連接,這就是我獲得這些數據的方式。但是,問題依然存在。這個數組如何轉換爲JSON?我相信問題是受保護的成員變量的結果。 – 2014-12-08 03:00:01

+0

你能夠實現[JsonSerializable](http://php.net/manual/en/jsonserializable.jsonserialize.php)嗎?如果不是,您可以通過反射來檢索值。 – Rangad 2014-12-08 03:03:23

回答

0

最後,這是我選擇使用的溶液:

function objectToArray ($object) { 
    if(!is_object($object) && !is_array($object)) 
     return $object; 

    return array_map('objectToArray', (array) $object); 
} 

從這裏:How do I convert an object to an array?

對於多維陣列(和不包含受保護變量的對象的陣列),json_encode( )效果很好。

0

你需要寫一個函數爲類「QuickBooks_IPP_Object_MetaData」,並得到一個數組。你可能會看到這個問題,Serializing PHP object to JSON

+0

我想這可能是我的答案。讓我試着讓它工作,如果我能解決我的問題,我會標記你的答案是正確的。謝謝。 – 2014-12-08 03:02:47

1

如果這些類無法實現JsonSerializable你可以使用反射訪問受保護的屬性。

實施例:

class Publisher { 
    protected $name; 
    protected $id; 
    public function __construct($id, $name) { 
     $this->id = $id; 
     $this->name = $name; 
    } 
} 

function getPublisherExportData($publishers) { 
    $out = []; 
    foreach($publishers as $publisher) { 
     $currentPData = []; 
     $refl = new ReflectionClass($publisher); 
     $props = $refl->getProperties(ReflectionProperty::IS_PROTECTED); 
     foreach($props as $prop) { 
      $prop->setAccessible(true); 
      $currentPData[$prop->getName()] = $prop->getValue($publisher); 
     } 
     $out[] = $currentPData; 
    } 

    return $out; 
} 

var_dump(json_encode(getPublisherExportData([new Publisher(12, 'My Name'), new Publisher(15, 'NameProperty')]), JSON_PRETTY_PRINT)); 

會導致:

string(121) "[ { "name": "My Name", "id": 12 }, { "name": "NameProperty", "id": 15 } ]" 

但是,您將需要自己處理對象圖和條件導出。根據所需的json輸出,您可能會考慮將$out替換爲stdClass的實例,並使用$out->{$prop->getName()} = $val語法編寫它。

如果你能夠使用你的自定義實現,你可以使用get_object_vars($this)添加一個方法來避免上面的大部分代碼,但是如果你可以這樣做,你應該考慮實現JsonSerializable這是專門用於該任務。