如何将xml响应更改为json?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将xml响应更改为json?相关的知识,希望对你有一定的参考价值。

我正在使用davidsadler's PHP ebay SDK在我的php应用程序中集成ebay。每个响应都是这种类型的xml。我已经回应了

 before this response to see it clearly.

    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和simplexml_load_file等函数的json_encode函数将此响应转换为json。但转换的数据总是空的。任何帮助,将不胜感激。

更新:问题已在eBay PHP SDK的新版本中得到解决0.1.3他在BaseType中添加了一个新方法toArray(),该方法将响应转换为关联数组。感谢davidsadler。链接到changelog 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上的相关文章,其中详细介绍了这些转换的问题,我认为您会发现这些问题很有启发性。 O'Reilly主持该文章的事实应该表明Stefan的解决方案具有优点。

以上是关于如何将xml响应更改为json?的主要内容,如果未能解决你的问题,请参考以下文章

将 Web 表单中的 XML 响应更改为 CSV 文件

将测试 XML 响应更改为 Bean 响应 Java

AJAX 响应:数据(JSON、XML)还是 HTML 片段? [关闭]

如何从片段中的 JSON 响应中的对象获取数据

RestKit:如何将 RKRequest 正文字符串从 JSON 更改为纯文本?

Android:将“ViewPager”动画从片段更改为片段