我有一个包含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不能使用"受保护的成员变量。"如果我无法访问创建它们的类,是否可以将这些变量声明为公共变量?
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 :(得分:1)
如果这些类无法实现JsonSerializable,您可以使用Reflection访问受保护的属性。
示例:
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那个任务。
答案 1 :(得分:1)
最好的方法是实现JsonSerializable类并定义jsonSerialize()函数
示例:
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient({region: "ap-southeast-1"});
const params = {
TableName : 'tablename'
}
async function listItems(){
try {
const data = await docClient.scan(params).promise()
return data
} catch (err) {
return err
}
}
exports.handler = async (event, context) => {
try {
const data = await listItems()
return { body: JSON.stringify(data) }
} catch (err) {
return { error: err }
}
}
答案 2 :(得分:0)
你需要为你的班级编写一个函数" QuickBooks_IPP_Object_MetaData'并获得一个数组。你可能会看到这个问题Serializing PHP object to JSON
答案 3 :(得分: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()效果很好。