Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 从json url文件读取并计算一个值_Jquery_Json - Fatal编程技术网

Jquery 从json url文件读取并计算一个值

Jquery 从json url文件读取并计算一个值,jquery,json,Jquery,Json,我想展示一下我的0.128欧元硬币是多少欧元。 如何计算欧元价格乘以0.128 <!DOCTYPE html> <html lang="en"> <head> <title>JavaScript - read JSON from URL</title> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> </head

我想展示一下我的0.128欧元硬币是多少欧元。 如何计算欧元价格乘以0.128

<!DOCTYPE html> <html lang="en"> <head> <title>JavaScript - read JSON from URL</title>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> </head>

<body>
    <div class="mypanel"></div>

    <script>
    $.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR', function(data) {

        var text = `0.128 * parseFloat(${data.data.quotes.EUR.price})`


        $(".mypanel").html(text);
    });
    </script>
     </body> </html>
JavaScript-从URL读取JSON
$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR,函数(数据){
var text=`0.128*parseFloat(${data.data.quotes.EUR.price})`
$(“.mypanel”).html(文本);
});

在函数中,您不应以类似JQuery的方式使用
数据。内部是纯JavaScript,如:

$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR,函数(数据){
var res=0.128*parseFloat(data.data.quotes.EUR.price)
$(“.mypanel”).html(res);
});

JavaScript-从URL读取JSON

您不必解析结果:结果已经是浮点类型

如果你想的话,可以把它围起来

以下是工作代码:

JavaScript-从URL读取JSON


$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR,函数(数据){
var text=0.128*data.data.quotes.EUR.price
$(“.mypanel”).html(文本);
});
和一个可玩的JSFIDLE:


var text=0.128*data.data.quotes.EUR.price?为什么使用模板文本…反勾选?
<script>
$.getJSON('https://api.coinmarketcap.com/v2/ticker/2/?convert=EUR', function(data) {

    var text = 0.128 *  data.data.quotes.EUR.price

    $(".mypanel").html(text);
});

</script>
 </body> </html>