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

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题