duanlin1933 2019-08-11 14:49
浏览 142
已采纳

json_encode在php中丢失了一些对象值

I have an object $myObject; I'm trying to return php object as json, but it loses some data. This is how $myObject looks:

CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList Object
(
    [priceListId:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 32
    [amounts:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
        (
            [0] => 1000
            [1] => 2000
            [2] => 3000
            [3] => 4000
            [4] => 5000
            [5] => 6000
            [6] => 7000
        )

    [amountsKeys:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
        (
            [1000] => 0
            [2000] => 1
            [3000] => 2
            [4000] => 3
            [5000] => 4
            [6000] => 5
            [7000] => 6
        )

    [periods:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
        (
            [0] => 10
            [1] => 15
            [2] => 20
            [3] => 25
            [4] => 30
        )

    [periodsKeys:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
        (
            [10] => 0
            [15] => 1
            [20] => 2
            [25] => 3
            [30] => 4
        )

    [amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 7000
    [period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => 30
    [prices:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => Array
        (
            [30] => Array
                (
                    [7000] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price Object
                        (
                            [period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 30
                            [amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 7000
                            [charge:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1580
                            [interest:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
                            [administrativeFee:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
                            [payment:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
                            [annualPercentageRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1089.6
                            [annualInterestRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 274.62
                            [schedule:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 
                        )

                )

        )

    [settings:CreditOnline\Bundle\CreditOnlineBundle\PriceList\PriceList:private] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings Object
        (
            [mode:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => credits
            [preset:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => 
            [implementation:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => matrix
            [defaults:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Settings:private] => Array
                (
                    [period] => 
                    [amount] => 
                )

        )

)

After json_encode($myObject); new data looks like this (screenshot for better json view):

screenshot of returned json

Why information I wrote below is missing and how to access it?

Missing stuff:

[7000] => CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price Object
                        (
                            [period:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 30
                            [amount:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 7000
                            [charge:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1580
                            [interest:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
                            [administrativeFee:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
                            [payment:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 0
                            [annualPercentageRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 1089.6
                            [annualInterestRate:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 274.62
                            [schedule:CreditOnline\Bundle\CreditOnlineBundle\PriceList\Price:private] => 
                        )
  • 写回答

1条回答 默认 最新

  • dongyong8098 2019-08-11 16:24
    关注

    The problem is json_encode can not access private properties.

    Here are some workarounds you could do:

    1. Convert the properties to be public
    2. You could use a reflection class to convert the properties accessibility before you convert it

    Example:

    $refObject = new ReflectionObject( $obj );
    $refProperty = $refObject->getProperty( 'property' );
    $refProperty->setAccessible( true );
    
    1. You could loop through the properties of the object using a Reflection object and call all the getters converting the object into an array structure that could be easily translated

    Refelection Class Documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求会6sv辐射传输模型,辅导(可py6s🙏🏻有偿
  • ¥15 .xla后缀的文件拖到excel里什么内容也没有怎么办
  • ¥20 Workbench中Mechanical打不开、闪退是什么原因?
  • ¥240 MapReduce应用实践 学生课程
  • ¥15 hlss视频显示AUTHORITY_INVALID
  • ¥15 MAX9296A+MAX96717,美信gmsl解串有人做过吗?
  • ¥15 求帮我解决一下inode 爆满的问题(有偿)
  • ¥15 关于#vscode#的问题:布料滤波算法中C++实现pcl在Vscode中pcl库没有#include <pcl>
  • ¥15 fpga:ov5640采集tft显示
  • ¥100 python怎么连接wxSQLite3加密的数据库