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

时间:2013-07-05 16:33:21

标签: php javascript jquery json

我有一个具有私有属性的对象(可通过getter和setter访问,并在_sleep magic函数中指定)。其中一些私有属性实际上是对象数组,它们本身具有相同方式的私有属性。其中一些属性是对象的数组,依此类推,沿着兔子洞走下去。

我想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”参数,它是一个包含三个插入对象的数组,本身不是一个带有三个插入子节点的参数,而是三个“插入”参数。

这是对的吗?

当我尝试使用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)

我觉得我犯了一些简单的错误,但我不能为我的生活看到它。

请帮帮忙?我被困在我刚刚看了太久的路上。

2 个答案:

答案 0 :(得分:2)

尝试:

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

答案 1 :(得分:0)

您的数据是一个包含1个元素的数组,您在javascript中尝试了以下内容:

data[0].accountId;
data[0].insertions;