2

我有一个相当复杂的对象,它用 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 生成的完整对象然后回显:

{"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
                                                )
                                        )
                                )
                        )
                )
        )
4

0 回答 0