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

标签 php javascript jquery json

我有一个具有私有(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/

相关文章:

php - Phalcon 性能相关查询

javascript - WebGL2 : incomplete framebuffer

javascript - 使用 Javascript/Jquery 的本地存储(不使用 HTML5)

javascript - 如何将json解析成嵌套的html列表结构

jquery - 单击jquery和css后如何在图像上添加图标

javascript - 如何根据下拉选择值使用 Javascript 更改图像?

php - 如何使用 Gdata 从 Picasa 获取实际图像

php-mysql 数据表与数据表服务器处理 ssp.class.php 脚本

php - xampp 中 phpmyadmin 中的 token 不匹配

javascript - 是否可以在 HTML5 MSE 中的视频轨道之间切换?