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

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

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

报告相同问题?

悬赏问题

  • ¥15 Arduino,利用modbus的RS485协议,进行对外置的温湿度传感器进行数据读取
  • ¥15 vhdl+MODELSIM
  • ¥20 simulink中怎么使用solve函数?
  • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
  • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
  • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
  • ¥15 知识蒸馏实战博客问题
  • ¥15 用PLC设计纸袋糊底机送料系统
  • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
  • ¥15 用C语言输入方程怎么