如何将xml响应更改为json



我正在使用davidsadler的PHP ebay SDK将ebay集成到我的PHP应用程序中。每个响应都是这种类型的xml形式。在这个回应之前,我已经回应了

,以清楚地看到它。
    DTSeBaySDKTradingTypesItemArrayType Object
(
    [values:DTSeBaySDKTypesBaseType:private] => Array
        (
            [Item] => DTSeBaySDKTypesUnboundType Object
                (
                    [data:DTSeBaySDKTypesUnboundType:private] => Array
                        (
                            [0] => DTSeBaySDKTradingTypesItemType Object
                                (
                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                        (
                                            [BuyItNowPrice] => DTSeBaySDKTradingTypesAmountType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [currencyID] => USD
                                                            [value] => 100
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                            [ItemID] => 110163537686
                                            [ListingDetails] => DTSeBaySDKTradingTypesListingDetailsType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [StartTime] => DateTime Object
                                                                (
                                                                    [date] => 2015-06-16 17:20:21.000000
                                                                    [timezone_type] => 2
                                                                    [timezone] => Z
                                                                )
                                                            [ViewItemURL] => http://cgi.sandbox.ebay.com/Test-/110163537686
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                            [ListingDuration] => GTC
                                            [ListingType] => FixedPriceItem
                                            [Quantity] => 100
                                            [SellingStatus] => DTSeBaySDKTradingTypesSellingStatusType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [CurrentPrice] => DTSeBaySDKTradingTypesAmountType Object
                                                                (
                                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                                        (
                                                                            [currencyID] => USD
                                                                            [value] => 100
                                                                        )
                                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                                        (
                                                                            [data] => 
                                                                            [mimeType] => 
                                                                        )
                                                                )
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                            [ShippingDetails] => DTSeBaySDKTradingTypesShippingDetailsType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [ShippingServiceOptions] => DTSeBaySDKTypesUnboundType Object
                                                                (
                                                                    [data:DTSeBaySDKTypesUnboundType:private] => Array
                                                                        (
                                                                            [0] => DTSeBaySDKTradingTypesShippingServiceOptionsType Object
                                                                                (
                                                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                                                        (
                                                                                            [ShippingServiceCost] => DTSeBaySDKTradingTypesAmountType Object
                                                                                                (
                                                                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                                                                        (
                                                                                                            [currencyID] => USD
                                                                                                            [value] => 2
                                                                                                        )
                                                                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                                                                        (
                                                                                                            [data] => 
                                                                                                            [mimeType] => 
                                                                                                        )
                                                                                                )
                                                                                        )
                                                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                                                        (
                                                                                            [data] => 
                                                                                            [mimeType] => 
                                                                                        )
                                                                                )
                                                                        )
                                                                    [position:DTSeBaySDKTypesUnboundType:private] => 0
                                                                    [class:DTSeBaySDKTypesUnboundType:private] => DTSeBaySDKTradingTypesShippingDetailsType
                                                                    [property:DTSeBaySDKTypesUnboundType:private] => ShippingServiceOptions
                                                                    [expectedType:DTSeBaySDKTypesUnboundType:private] => DTSeBaySDKTradingTypesShippingServiceOptionsType
                                                                )
                                                            [ShippingType] => Flat
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                            [TimeLeft] => P22DT21H24M4S
                                            [Title] => Test
                                            [QuantityAvailable] => 100
                                            [SKU] => ABC-001
                                            [PictureDetails] => DTSeBaySDKTradingTypesPictureDetailsType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [GalleryURL] => http://thumbs.sandbox.ebaystatic.com/pict/1101635376866464.jpg
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                            [SellerProfiles] => DTSeBaySDKTradingTypesSellerProfilesType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                        )
                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                        (
                                            [data] => 
                                            [mimeType] => 
                                        )
                                )
                            [1] => DTSeBaySDKTradingTypesItemType Object
                                (
                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                        (
                                            [BuyItNowPrice] => DTSeBaySDKTradingTypesAmountType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [currencyID] => USD
                                                            [value] => 89
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                            [ItemID] => 110163540851
                                            [ListingDetails] => DTSeBaySDKTradingTypesListingDetailsType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [StartTime] => DateTime Object
                                                                (
                                                                    [date] => 2015-06-16 20:33:27.000000
                                                                    [timezone_type] => 2
                                                                    [timezone] => Z
                                                                )
                                                            [ViewItemURL] => http://cgi.sandbox.ebay.com/watch-/110163540851
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                            [ListingDuration] => GTC
                                            [ListingType] => FixedPriceItem
                                            [Quantity] => 20
                                            [SellingStatus] => DTSeBaySDKTradingTypesSellingStatusType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [CurrentPrice] => DTSeBaySDKTradingTypesAmountType Object
                                                                (
                                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                                        (
                                                                            [currencyID] => USD
                                                                            [value] => 89
                                                                        )
                                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                                        (
                                                                            [data] => 
                                                                            [mimeType] => 
                                                                        )
                                                                )
                                                        )
                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )
                                                )
                                            [ShippingDetails] => DTSeBaySDKTradingTypesShippingDetailsType Object
                                                (
                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                        (
                                                            [ShippingServiceOptions] => DTSeBaySDKTypesUnboundType Object
                                                                (
                                                                    [data:DTSeBaySDKTypesUnboundType:private] => Array
                                                                        (
                                                                            [0] => DTSeBaySDKTradingTypesShippingServiceOptionsType Object
                                                                                (
                                                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                                                        (
                                                                                            [ShippingServiceCost] => DTSeBaySDKTradingTypesAmountType Object
                                                                                                (
                                                                                                    [values:DTSeBaySDKTypesBaseType:private] => Array
                                                                                                        (
                                                                                                            [currencyID] => USD
                                                                                                            [value] => 2
                                                                                                        )
                                                                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                                                                        (
                                                                                                            [data] => 
                                                                                                            [mimeType] => 
                                                                                                        )
                                                                                                )
                                                                                        )
                                                                                    [attachment:DTSeBaySDKTypesBaseType:private] => Array
                                                                                        (
                                                                                            [data] => 
                                                                                            [mimeType] => 
                                                                                        )
                                                                                )

我尝试使用php的json_encode函数和simplexml_load_file等函数将此响应转换为json。但是转换后的数据总是空的。如有任何帮助,我们将不胜感激。

更新:问题已在新版易趣PHP SDK中得到解决,该SDK 0.1.3他在BaseType中添加了一个新方法toArray(),它将响应转换为关联的数组。感谢戴维斯。链接到变更日志https://github.com/davidtsadler/ebay-sdk/blob/master/CHANGELOG.md#013---2015-06-20

试试这个http://goessner.net/download/prj/jsonxml/

请务必阅读xml.com O'Reilly网站上的随附文章http://www.xml.com/pub/a/2006/05/31/converting-between-xml-and-json.html,详细介绍了这些转换的问题,我想你会觉得很有启发性。奥主持这篇文章的事实应该表明Stefan的解决方案是有价值的。

相关内容

  • 没有找到相关文章

最新更新