개인 속성 (getters 및 setters를 통해 액세스 할 수 있으며 _sleep 마법 함수에서 지정)이있는 객체가 있습니다. 이러한 사적 속성 중 일부는 실제로 객체 배열이며, 그 자체로 동일한 방식으로 사적 속성을가집니다. 그 속성 중 일부는 객체의 배열입니다. 등등, 우리가가는 토끼의 구멍 아래로.jQuery를 사용하여 복잡한 json 객체에 올바르게 액세스 할 수 없습니다.
내가 부모 객체와 객체의 아이 배열의 모든 등 등 여기
을로 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"
}
]
}
]
나는 세 가지 삽입 객체의 배열 부모 개체의 "삽입"매개 변수가 세 삽입 아이들이 아니라 세 "삽입"매개 변수를 사용하여 하나 개의 매개 변수는 그 자체라고 여기를 참조하십시오.
이게 맞습니까?
자바 스크립트를 사용하여 클라이언트 쪽에서 개별 삽입 개체에 액세스하려고하면 운이 가지 않습니다.
[The JSON]
undefined
undefined
Type error, e is undefined (jquery.min.js)
나는 몇 가지 간단한 실수를 만들고있어 그런 의미,하지만 난 내 인생에 그것을 볼 수 없기 :
// 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);
});
}
});
이
인쇄입니다.도와주세요. 나는 내가 너무 오랫동안 이것을 보았던 방식으로 붙어있다.
* "이의 가독성을 돕기 위해, 내가 변환합니다 JSON을 XML로 변환 "* 아마도이 문장은 역사상 처음으로 발췌되었습니다. : D –
나는 XML을 제거하고 그것을 읽을 수 있도록 JSON을 prettified했다. http://jsonformatter.curiousconcept.com/이 좋은 도구입니다. – lonesomeday
롤 감사합니다 얘들 아! <3 – Lurk21