我有一个相当复杂的对象,它是用PHP构造的,并使用JSON发送给JQuery。 这个过程非常有效,但到目前为止我在解决这个问题上没有运气。出于某种原因,我没有看到这里发生了什么 - 仍然在2天后,所以有人可以指出我正确的方向。
我有许多构建对象的函数。它们都运行良好,在流程结束时,JSON在JQuery中成功解析。从数据库中获取数据并执行一些简单算法的其中一个函数始终导致JSON解析错误。此函数将stdClass子对象数组添加为主对象的属性。通过在构造过程中不调用此函数,JSON解析错误就消失了。
这是我到目前为止所发现的(为了简单起见,我已将问题区域隔离开来)
//The $dataObject is derived from a database with:
$dataObject = $db->loadObjectList(); // Using Joomla framework
001: // The section of code in php which illustrates the problem
002: $jsonOutput = json_encode($dataObject);
003: dump($jsonOutput, '$jsonOutput');
004: echo $jsonOutput;
005: // Here are the results:
006: // Dumping the array of stdClass objects to JDump produces the following:
007: $jsonOutput = {"resourceList":
008: [
009: {
010: "assetCategory":"Accommodation",
011: "assetCount":"1"
012: },
013: {
014: "assetCategory":"Equipment",
015: "assetCount":"3"
016: }
017: ]
018: }
019: // This is what is produced on screen with echo command.
020: {"resourceList":[,]}
// The individual sub-objects are not produced in the output.
我不知道为什么会这样。我尝试过不同的密钥名称,子对象定义名称等无济于事。
在整个模型构造函数中,我在同一位置调用了其他函数而不是这个函数,并且没有问题。我不知何故仅限于此功能。
将原始JSON转储到JDump实用程序中,产生预期的结果,但使用echo在浏览器的屏幕上产生结果,产生一个格式错误的“无”数组,导致JQuery中的JSON解析错误线。
有没有人对此有任何想法?
提前致谢
问题的更多例子:
使用json_encode生成的完整对象然后是echo:
{"568":{"logId":"568","firstName":"Jonathan","lastName":"Harper","fullName":"Jonathan Harper","email":"jih13@hotmail.com","telephone":"07817167228","countryId":"234","country":"United Kingdom","address":"","subject":"Hi","message":"Hello.\n\nHope you're all well there. I actually came to The Casa in June last year and I've got some free time in October to come over for the surf 'n' stay for about 5 days.\n\nI just wanted to confirm the cost for that time.\n\nThanks for your help.\n\nJonathan.","partySize":"1","itemToken":"-","parentToken":null,"packageGroupId":"2","packageGroup":"Surf n Stay","packageNameId":"5","packageName":"7 nights - Private Single Room - 5 days Surf course","fromDate":"2013-10-19","toDate":"2013-11-02","nights":"14","ipAddress":"-","terms":"0","createdDate":"2013-09-17","logStatusId":"1","logStatus":"Pending","statusOrder":"1","displayParams":"background-color:#FF8080;font-weight:700;","groupList":[{"enqLogDetId":"700","order":"0","enqLogId":"568","firstName":"Jonathan","lastName":"Harper","gender":"Male","fitnessLevel":"","dateOfBirth":"0000-00-00","ageGroupId":"1","ageGroup":"Unknown","address":"","countryId":"234","country":"United Kingdom","email":"jih13@hotmail.com","promote":"0","notes":null,"statusId":"2","status":"Current","age":null,"fullName":"Jonathan Harper"}],"responseList":[{"responseId":"460","responseEnqLogId":"568","responseAdminUserId":"1","response":"Hello.\n\nHope you're all well there. I actually came to The Casa in June last year and I've got some free time in October to come over for the surf 'n stay for about 5 days.\n\nI just wanted to confirm the cost for that time.\n\nThanks for your help.\n\nJonathan.","responseToken":"-","responseDate":"2013-11-14","responseMode":"Details Modified","responsePackageGroupId":"2","responsePackageNameId":"5","responseFromDate":"2013-10-19","responseToDate":"2013-11-02","responsePartySize":"1","responseNotes":"Enter note here","responseStatusId":"2","responsePackageGroup":"Surf n Stay","responsePackageName":"7 nights - Private Single Room - 5 days Surf course","responseStatus":"Current","responseUserId":"42","responseLogStatus":"Pending","responseLogStatusId":"1","responseUserName":"Joe Stokes","seasonPrices":[{"seasonName":"2013 Mid Season","seasonCode":"13-Mid","seasonStart":"2013-10-01","seasonEnd":"2013-10-31","book_in":"2013-10-19","book_out":"2013-11-02","seasonPrice":"410.00","seasonNightPeriod":"7","billingCriterium":"Per room per period","inventoryId":"6","unitCapacity":"1.0000","bedCount":"1.0000","unitUseage":"Private","bookingDays":14,"seasonDays":12,"seasonSplitPrice":702.86},{"seasonName":"2013 Low Season","seasonCode":"13-Low","seasonStart":"2013-11-01","seasonEnd":"2013-11-30","book_in":"2013-10-19","book_out":"2013-11-02","seasonPrice":"340.00","seasonNightPeriod":"7","billingCriterium":"Per room per period","inventoryId":"6","unitCapacity":"1.0000","bedCount":"1.0000","unitUseage":"Private","bookingDays":14,"seasonDays":2,"seasonSplitPrice":97.14}],"resourceList":[,]}]}}
您可以在最后清楚地看到格式错误的对象。
echo '<pre>'.print_r($dataObject, true).'</pre>';
的输出产生以下内容:
Array
(
[568] => stdClass Object
(
[logId] => 568
[firstName] => Jonathan
[lastName] => Harper
[fullName] => Jonathan Harper
[email] => jih13@hotmail.com
[telephone] => 07817167228
[countryId] => 234
[country] => United Kingdom
[address] =>
[subject] => Hi
[message] => Hello.
[partySize] => 1
[itemToken] => -
[parentToken] =>
[packageGroupId] => 2
[packageGroup] => Surf n Stay
[packageNameId] => 5
[packageName] => 7 nights - Private Single Room - 5 days Surf course
[fromDate] => 2013-10-19
[toDate] => 2013-11-02
[nights] => 14
[ipAddress] => -
[terms] => 0
[createdDate] => 2013-09-17
[logStatusId] => 1
[logStatus] => Pending
[statusOrder] => 1
[displayParams] => background-color:#FF8080;font-weight:700;
[groupList] => Array
(
[0] => stdClass Object
(
[enqLogDetId] => 700
[order] => 0
[enqLogId] => 568
[firstName] => Jonathan
[lastName] => Harper
[gender] => Male
[fitnessLevel] =>
[dateOfBirth] => 0000-00-00
[ageGroupId] => 1
[ageGroup] => Unknown
[address] =>
[countryId] => 234
[country] => United Kingdom
[email] => jih13@hotmail.com
[promote] => 0
[notes] =>
[statusId] => 2
[status] => Current
[age] =>
[fullName] => Jonathan Harper
)
)
[responseList] => Array
(
[0] => stdClass Object
(
[responseId] => 460
[responseEnqLogId] => 568
[responseAdminUserId] => 1
[response] => Hello.
[responseToken] => -
[responseDate] => 2013-11-14
[responseMode] => Details Modified
[responsePackageGroupId] => 2
[responsePackageNameId] => 5
[responseFromDate] => 2013-10-19
[responseToDate] => 2013-11-02
[responsePartySize] => 1
[responseNotes] => Enter note here
[responseStatusId] => 2
[responsePackageGroup] => Surf n Stay
[responsePackageName] => 7 nights - Private Single Room - 5 days Surf course
[responseStatus] => Current
[responseUserId] => 42
[responseLogStatus] => Pending
[responseLogStatusId] => 1
[responseUserName] => Joe Stokes
[seasonPrices] => Array
(
[0] => stdClass Object
(
[seasonName] => 2013 Mid Season
[seasonCode] => 13-Mid
[seasonStart] => 2013-10-01
[seasonEnd] => 2013-10-31
[book_in] => 2013-10-19
[book_out] => 2013-11-02
[seasonPrice] => 410.00
[seasonNightPeriod] => 7
[billingCriterium] => Per room per period
[inventoryId] => 6
[unitCapacity] => 1.0000
[bedCount] => 1.0000
[unitUseage] => Private
[bookingDays] => 14
[seasonDays] => 12
[seasonSplitPrice] => 702.86
)
[1] => stdClass Object
(
[seasonName] => 2013 Low Season
[seasonCode] => 13-Low
[seasonStart] => 2013-11-01
[seasonEnd] => 2013-11-30
[book_in] => 2013-10-19
[book_out] => 2013-11-02
[seasonPrice] => 340.00
[seasonNightPeriod] => 7
[billingCriterium] => Per room per period
[inventoryId] => 6
[unitCapacity] => 1.0000
[bedCount] => 1.0000
[unitUseage] => Private
[bookingDays] => 14
[seasonDays] => 2
[seasonSplitPrice] => 97.14
)
)
[resourceList] => Array
(
[0] => stdClass Object
(
[category] => Accommodation
[available] => 1
)
[1] => stdClass Object
(
[category] => Equipment
[available] => 3
)
)
)
)
)
)