gpt4 book ai didi

php - 无法使用 jQuery 正确访问复杂的 json 对象

转载 作者:行者123 更新时间:2023-12-02 18:34:13 24 4
gpt4 key购买 nike

我有一个具有私有(private)属性的对象(可通过 getter 和 setter 访问,并在 _sleep 魔术函数中指定)。其中一些私有(private)属性实际上是对象数组,它们本身以相同的方式具有私有(private)属性。其中一些属性是对象数组,等等,我们进入兔子洞。

我想对父对象和所有子对象数组等进行 json_encode。

这是我到目前为止的功能:

json_encode(recursiveFlatten($contract));

function recursiveFlatten($object) {
$arr = array();
$return = array();

if (!is_array($object)) {
error_log("Preparing to flatten " . get_class($object));
$arr[] = $object;
} else {
error_log("Preparing to flatten array");
$arr = $object;
}

foreach ($arr as $object) {

error_log("Flattening " . get_class($object));

$flattenedObject = array();
foreach (get_class_methods(get_class($object)) as $method) {
error_log("Should I flatten " . $method . "?");
if (startsWith($method, "get")) {
error_log("Yes, flattening " . $method);
$parameter = lcfirst(substr($method, 3));
$value = $object->$method();

if (is_array($value)) {
error_log($method . " gives " . $parameter . " which yields an array value... recursing");
$value = recursiveFlatten($value);
error_log("Recursion yielded the following value: " . $value);
}

error_log($method . " gives " . $parameter . " which is not an array so it's value is " . $value);

error_log("Assign " . $parameter . " to flattenedObject with value " . $value);

$flattenedObject[$parameter] = $value;
}
}

$return[] = $flattenedObject;
}

return $return;
}

但这返回的 json 对我来说看起来不正确。

[
{
"accountId":"7",
"billingContactId":"1",
"designFeeId":"295",
"id":"1",
"isDeleted":"0",
"leadSourceId":"1",
"managerId":"415",
"notes":"A note",
"prodContactId":"1",
"statusId":"1",
"tradedValue":"0",
"createdById":"415",
"createdDate":"2013-07-02 10:05:53",
"designFeeValue":"295",
"doInvoice":"0",
"isPaper":"1",
"primaryContactId":"1",
"firstMonthDisplay":"01\/2014",
"managerDisplayName":"Lynn Owens",
"numInsertions":3,
"statusText":"Proposal",
"totalValue":75,
"paidValue":50,
"unpaidValue":25,
"insertions":[
{
"adId":"1",
"contractId":"1",
"createdById":"415",
"createdDate":"2013-07-02 16:09:19",
"earlyOut":"0",
"id":"1",
"isCanceled":"0",
"magazineId":"1",
"month":"1",
"notes":"insertion one",
"paidValue":"25",
"value":"25",
"year":"2014"
},
{
"adId":"1",
"contractId":"1",
"createdById":"415",
"createdDate":"2013-07-02 16:10:03",
"earlyOut":"0",
"id":"2",
"isCanceled":"0",
"magazineId":"1",
"month":"2",
"notes":"insertion two",
"paidValue":"25",
"value":"25",
"year":"2014"
},
{
"adId":"1",
"contractId":"1",
"createdById":"415",
"createdDate":"2013-07-02 16:10:03",
"earlyOut":"0",
"id":"3",
"isCanceled":"0",
"magazineId":"1",
"month":"3",
"notes":"insertion three",
"paidValue":"0",
"value":"25",
"year":"2014"
}
]
}
]

我在这里看到父对象的“insertions”参数是一个由三个插入对象组成的数组,它本身并不是具有三个插入子对象的一个​​参数,而是三个“insertions”参数。

这是对的吗?

当我尝试使用 JavaScript 访问客户端的各个插入对象时,我没有任何运气。

// Set the account details
$.ajax({
type: 'POST',
url: 'ajaxController.php',
dataType: 'json',
data: {
e: "getContractById",
contractId: selectedContractId
},
success: function (data, textStatus, jqXHR) {
console.log(data);
console.log(data.accountId);
console.log(data.insertions);
$.each(data.insertions, function(key, insertion) {
console.log(insertion.notes);
});
}
});

这是打印:

[The JSON]
undefined
undefined
Type error, e is undefined (jquery.min.js)

我感觉我犯了一些简单的错误,但我一生都看不到它。

请帮忙?我陷入了这样的困境,因为我已经看这个问题太久了。

最佳答案

尝试:

data[0].accountId
// or
data[0]['accountId']

关于php - 无法使用 jQuery 正确访问复杂的 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17493357/

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