如何将xml响应更改为json?

Posted

技术标签:

【中文标题】如何将xml响应更改为json?【英文标题】:How to change xml response to json? 【发布时间】:2015-09-09 19:43:36 【问题描述】:

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

 在此响应之前可以清楚地看到它。

    DTS\eBaySDK\Trading\Types\ItemArrayType Object
(
    [values:DTS\eBaySDK\Types\BaseType:private] => Array
        (
            [Item] => DTS\eBaySDK\Types\UnboundType Object
                (
                    [data:DTS\eBaySDK\Types\UnboundType:private] => Array
                        (
                            [0] => DTS\eBaySDK\Trading\Types\ItemType Object
                                (
                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                        (
                                            [BuyItNowPrice] => DTS\eBaySDK\Trading\Types\AmountType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [currencyID] => USD
                                                            [value] => 100
                                                        )

                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                            [ItemID] => 110163537686
                                            [ListingDetails] => DTS\eBaySDK\Trading\Types\ListingDetailsType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType: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:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                            [ListingDuration] => GTC
                                            [ListingType] => FixedPriceItem
                                            [Quantity] => 100
                                            [SellingStatus] => DTS\eBaySDK\Trading\Types\SellingStatusType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [CurrentPrice] => DTS\eBaySDK\Trading\Types\AmountType Object
                                                                (
                                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                        (
                                                                            [currencyID] => USD
                                                                            [value] => 100
                                                                        )

                                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                        (
                                                                            [data] => 
                                                                            [mimeType] => 
                                                                        )

                                                                )

                                                        )

                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                            [ShippingDetails] => DTS\eBaySDK\Trading\Types\ShippingDetailsType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [ShippingServiceOptions] => DTS\eBaySDK\Types\UnboundType Object
                                                                (
                                                                    [data:DTS\eBaySDK\Types\UnboundType:private] => Array
                                                                        (
                                                                            [0] => DTS\eBaySDK\Trading\Types\ShippingServiceOptionsType Object
                                                                                (
                                                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                                        (
                                                                                            [ShippingServiceCost] => DTS\eBaySDK\Trading\Types\AmountType Object
                                                                                                (
                                                                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                                                        (
                                                                                                            [currencyID] => USD
                                                                                                            [value] => 2
                                                                                                        )

                                                                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                                                        (
                                                                                                            [data] => 
                                                                                                            [mimeType] => 
                                                                                                        )

                                                                                                )

                                                                                        )

                                                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                                        (
                                                                                            [data] => 
                                                                                            [mimeType] => 
                                                                                        )

                                                                                )

                                                                        )

                                                                    [position:DTS\eBaySDK\Types\UnboundType:private] => 0
                                                                    [class:DTS\eBaySDK\Types\UnboundType:private] => DTS\eBaySDK\Trading\Types\ShippingDetailsType
                                                                    [property:DTS\eBaySDK\Types\UnboundType:private] => ShippingServiceOptions
                                                                    [expectedType:DTS\eBaySDK\Types\UnboundType:private] => DTS\eBaySDK\Trading\Types\ShippingServiceOptionsType
                                                                )

                                                            [ShippingType] => Flat
                                                        )

                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                            [TimeLeft] => P22DT21H24M4S
                                            [Title] => Test
                                            [QuantityAvailable] => 100
                                            [SKU] => ABC-001
                                            [PictureDetails] => DTS\eBaySDK\Trading\Types\PictureDetailsType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [GalleryURL] => http://thumbs.sandbox.ebaystatic.com/pict/1101635376866464.jpg
                                                        )

                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                            [SellerProfiles] => DTS\eBaySDK\Trading\Types\SellerProfilesType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                        )

                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                        )

                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                        (
                                            [data] => 
                                            [mimeType] => 
                                        )

                                )

                            [1] => DTS\eBaySDK\Trading\Types\ItemType Object
                                (
                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                        (
                                            [BuyItNowPrice] => DTS\eBaySDK\Trading\Types\AmountType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [currencyID] => USD
                                                            [value] => 89
                                                        )

                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                            [ItemID] => 110163540851
                                            [ListingDetails] => DTS\eBaySDK\Trading\Types\ListingDetailsType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType: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:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                            [ListingDuration] => GTC
                                            [ListingType] => FixedPriceItem
                                            [Quantity] => 20
                                            [SellingStatus] => DTS\eBaySDK\Trading\Types\SellingStatusType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [CurrentPrice] => DTS\eBaySDK\Trading\Types\AmountType Object
                                                                (
                                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                        (
                                                                            [currencyID] => USD
                                                                            [value] => 89
                                                                        )

                                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                        (
                                                                            [data] => 
                                                                            [mimeType] => 
                                                                        )

                                                                )

                                                        )

                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [data] => 
                                                            [mimeType] => 
                                                        )

                                                )

                                            [ShippingDetails] => DTS\eBaySDK\Trading\Types\ShippingDetailsType Object
                                                (
                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                        (
                                                            [ShippingServiceOptions] => DTS\eBaySDK\Types\UnboundType Object
                                                                (
                                                                    [data:DTS\eBaySDK\Types\UnboundType:private] => Array
                                                                        (
                                                                            [0] => DTS\eBaySDK\Trading\Types\ShippingServiceOptionsType Object
                                                                                (
                                                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                                        (
                                                                                            [ShippingServiceCost] => DTS\eBaySDK\Trading\Types\AmountType Object
                                                                                                (
                                                                                                    [values:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                                                        (
                                                                                                            [currencyID] => USD
                                                                                                            [value] => 2
                                                                                                        )

                                                                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                                                        (
                                                                                                            [data] => 
                                                                                                            [mimeType] => 
                                                                                                        )

                                                                                                )

                                                                                        )

                                                                                    [attachment:DTS\eBaySDK\Types\BaseType:private] => Array
                                                                                        (
                                                                                            [data] => 
                                                                                            [mimeType] => 
                                                                                        )

                                                                                )

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

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

【问题讨论】:

我相信这是一个序列化的对象。尝试使用deserialize(); 你不能 JSON 这个响应。无论如何,这将是无效的 JSON。尽管如此,这是显示类、范围和数组。尝试反序列化它,看看你是否得到其他任何东西。 我刚收到来自 ebay SDK 的 davidsadler 创建者的回复。他刚刚添加了一种将响应转换为数组的新方法。无论如何感谢您的帮助。 【参考方案1】:

试试这个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?的主要内容,如果未能解决你的问题,请参考以下文章

Spring Security OAuth2 更改 JSON 错误响应格式

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

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

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

如何将Laravel输出的日期格式更改为JSON?

如何在不更改现有组件文件的情况下通过插件将日期类型更改为下拉列表