【问题标题】:Parsing from XML -> JSON -> Php Object从 XML 解析 -> JSON -> Php 对象
【发布时间】:2019-10-03 09:34:14
【问题描述】:

我将创建 php 对象以从响应 xml api 构建我自己的表。

我尝试将 API 响应 XML 转换为 Json,但无法将其转换为 php 对象。 PHP 对我来说是新的...

class ParserMy {
        public static function jsonToDebug($jsonText = '')
    {
        $arr = json_decode($jsonText, true);
        $html = "";
        if ($arr && is_array($arr)) {
            echo "<pre>".json_encode($arr,JSON_PRETTY_PRINT)."</pre>";
          //  $html .= self::_arrayToHtmlTableRecursive($arr);
        }
        return $html;
    }



}


$url = "....url....";
$xml = simplexml_load_file($url);

$json = json_encode($xml);

echo ParserMy::jsonToDebug($json);

我想将 json 或 xml 响应转换为对象数组,但还有更多属性......我无法管理。

示例从此json转换成:

{
    "station": [
        {
            "@attributes": {
                "id": "27",
                "co": "SH",
                "co_name": "Shell",
                "lat": "",
                "lng": "",
                "name": "Shell - QE",
                "address": "",
                "street_code": "",
                "highway_id": "",
                "highway": "",
                "zip": "",
                "city": "",
                "city_name": "",
                "provincia": "",
                "fuels": "bde",
                "phone": "",
                "status": "AP",
                "neighbour_distance": "",
                "insertion_date": "2008-01-01 00:00",
                "last_updated": "2013-12-31 00:00",
                "latest_price_change": "",
                "highres": "0",
                "photos": "0",
                "tags": "",
                "user": "qe"
            },
            "reports": {
                "report": [
                    {
                        "@attributes": {
                            "price": "1.666",
                            "date": "2018-04-09 00:00:00",
                            "service": "CS",
                            "fuel": "diesel"
                        }
                    },
                    {
                        "@attributes": {
                            "price": "1.782",
                            "date": "2018-04-09 00:00:00",
                            "service": "CS",
                            "fuel": "benzina"
                        }
                    }
                ]
            }
        }

    ]
}

变成唯一的对象别名

id: 27
co: SH
co_name:    Shell
name:   Shell - QE
address:    
street_code:    
zip:    
city:   
city_name:  
provincia:  
phone:  
fuels:  bde
tags:   
lat:    
lng:    
insertion_date: 2008-01-01 00:00
last_updated:   2013-12-31 00:00
latest_price_change:    
diesel (CS) 1.666 il 2018-04-09 00:00:00 cert:
benzina (CS)    1.782 il 2018-04-09 00:00:00 cert:

谢谢

【问题讨论】:

    标签: php json xml


    【解决方案1】:

    当您使用 simplexml_load_file() 解析 xml 时,它会返回一个 objectSimpleXMLElement,其属性包含 XML 文档中保存的数据,如果失败则返回 FALSE。

        $phpObject = simplexml_load_file($url);
    

    你可以直接投到array

        $array = (array) $phpObject;
    

    json:

        $json = json_encode($phpObject);
    

    你可以在这里查看一个工作示例http://ideone.com/MRMW1E

    【讨论】:

    • Ehm 我还有一个问题......但如果你尝试这个 url testcolan.it/campit/test_xml.php 你可以看到转储 $array 的输出。我会得到每个孩子的属性数组......我该怎么做?
    • @MaddalenaTinti 您应该发布一个新问题,因为这就是 stackoverflow 的工作原理。您的问题可能会对想问同样问题的其他人有所帮助。
    • 好吧……我解决了……如果你看上面的网址,它现在可以工作了……再次感谢@Aakash Tushar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-05-17
    • 1970-01-01
    相关资源
    最近更新 更多