gpt4 book ai didi

php - 如何将这个 PHP 对象数组转换为 json?

转载 作者:搜寻专家 更新时间:2023-10-31 21:32:44 25 4
gpt4 key购买 nike

我有一个包含 10 个对象的数组,我正在尝试将其转换为 JSON。该数组存储在名为 $invoices 的变量中。以下是仅简化为第一个对象的 var_dump($invoices) 的输出。

我试过以下方法:

$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 不能与“ protected 成员变量”一起使用。如果我无权访问创建它们的类,有没有办法将这些变量声明为公共(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) {
...
...

最佳答案

最好的方法是执行JsonSerializable类并定义 jsonSerialize() 函数

例子:

class MyObj implements JsonSerializable{
protected $id;
protected $name;

function getId() { return $this->$id; }
function getName() { return $this->$name; }

public function jsonSerialize() {
return [
'Id' => $this->getId(),
'Name' => $this->getName()
];
}
}

// $arrofobj is array of objects of type MyObj
json_encode($arrofobj); //this will convert array of objects to JSON

关于php - 如何将这个 PHP 对象数组转换为 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27350473/

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