简体   繁体   English

XML2JSON和检索数据

[英]XML2JSON and retrieving the data

Trying to make work this code. 尝试使此代码生效。

Using the function to convert XML 2 JSON 使用该函数转换XML 2 JSON

Sample Here: 此处示例:

<?php
class XmlToJson {
    public static function Parse($url) {
        $fileContents= file_get_contents($url);
        $fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
        $fileContents = trim(str_replace('"', "'", $fileContents));
        $simpleXml = simplexml_load_string($fileContents);
        $json = json_encode($simpleXml);
        return $json;
    }

}

$getXML = "http://api.devstate.de/json/divisas.xml";

    print XmlToJson::Parse($getXML);

?>

(By the way , the original code have some issues of malformed JSON so we add strict to the function) (顺便说一句,原始代码存在一些格式错误的JSON问题,因此我们对函数进行了严格的限制)

Seems that everything works correctly until this step. 似乎一切正常,直到此步骤。

We get a valid JSON, but when we tried to call it via jQuery or Angular we always get a UNDEFINED Data . 我们得到一个有效的JSON,但是当我们尝试通过jQuery或Angular对其进行调用时,我们总是会得到UNDEFINED Data

If I use: 如果我使用:

$(document).ready(function() {
    $.getJSON('convertedXML.php', function(json) {
        console.log(json.row);

    });
});

I get: 我得到:

https://www.dropbox.com/s/oq5a8av2etcwvo3/Screenshot%202015-04-08%2020.02.56.png https://www.dropbox.com/s/oq5a8av2etcwvo3/Screenshot%202015-04-08%2020.02.56.png

But what about json.row.$WHATTHEHELL.Divisa 但是json.row。$ WHATTHEHELL.Divisa呢?

Nothing happen , nothing is given back just UNDEFINED 什么都没有发生,什么也没有给,只是未定义的

What do you think? 你怎么看? , what are we doing wrong. ,我们在做什么错。

thanks 谢谢

Inside the $.getJSON block, if you're getting the correct response, then just use $.each to iterate inside it: $.getJSON块中,如果您得到正确的响应,则只需使用$.each在其中进行迭代:

$.each(json.row, function(i, e){
    console.log(e['@attributes'].Divisa); // and other properties
});

Sample Demo 样本演示

@Ghost the solution was fantastic! @Ghost解决方案很棒! thanks. 谢谢。

Just , any suggestion for a simplification here: 只是,这里有任何简化的建议:

<div>
    <ul id="currency-USD">
        <li class="currency-USD--name"></li>
        <li class="currency-USD--buy"></li>
        <li class="currency-USD--sell"></li>
    </ul>

    <ul id="currency-UE">
        <li class="currency-UE--name"></li>
        <li class="currency-UE--buy"></li>
        <li class="currency-UE--sell"></li>
    </ul>

    <ul id="currency-CAD">
        <li class="currency-CAD--name"></li>
        <li class="currency-CAD--buy"></li>
        <li class="currency-CAD--sell"></li>
    </ul>
</div>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<script>
    $(document).ready(function(){

        $.ajax({
            url: document.URL,
            type: 'POST',
            dataType: 'JSON',
            success: function(json) {
                var group = json.row;

                // alert('check the browser console');
                $('.currency-USD--name').html(group[0]['@attributes'].Divisa);
                $('.currency-USD--buy').html(group[0]['@attributes'].Compra);
                $('.currency-USD--sell').html(group[0]['@attributes'].Venta);

                $('.currency-UE--name').html(group[1]['@attributes'].Divisa);
                $('.currency-UE--buy').html(group[1]['@attributes'].Compra);
                $('.currency-UE--sell').html(group[1]['@attributes'].Venta);

                $('.currency-CAD--name').html(group[2]['@attributes'].Divisa);
                $('.currency-CAD--buy').html(group[2]['@attributes'].Compra);
                $('.currency-CAD--sell').html(group[2]['@attributes'].Venta);

                    // console.log(group[0]['@attributes'].Divisa);
                }
            });
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM