通过JSON检索数字并存储在变量中

时间:2014-02-28 01:22:02

标签: javascript jquery c json variables

好吧,所以我对编程很陌生,只学了一点基本的C。

https://api.vircurex.com/api/get_info_for_1_currency.json?base=DOGE&alt=BTC

http://software.intel.com/en-us/html5/articles/how-to-access-JSON-data-in-HTML5-apps

http://jsbin.com/IQUjUkiX/1/edit

基本上我想做的是从“http://pastebin.com/4SPcTFbQ(链接1)中检索json,并在某种变量中存储它返回的数字。 (有点像使用scanf()来检索数字并将其存储到变量中)

从我一直在研究它不能通过C完成,我相信必须通过javascript完成。在这个网站(请参阅pastebin Link 2),他们提供了这个例子,(请参阅pastebin Link 3),但当我尝试将他们的示例json替换为vircurex时它似乎不再起作用了。

非常感谢任何帮助!

以下是示例:

HTML

<h3>Get JSON with padding</h3>

<button onclick="doJSON1()">Get JSON</button>
<br>
<br>
<hr>

<h3>Get JSON with Access-Control-Allow-Origin header: *</h3>

<button onclick="doJSON2()">Get JSON</button>
<br>
<br>
<hr>

<h3>Get JSON with NO Access-Control-Allow-Origin header</h3>

<button onclick="doJSON3()">Get JSON</button> 

的Javascript

function doJSON1() {
    $.getJSON('http://time.jsontest.com/?alloworigin=false&callback=?', function (data) {
        alert(JSON.stringify(data))
    });
}

function doJSON2() {
    $.getJSON('http://time.jsontest.com/?alloworigin=true', function (data) {
        alert(JSON.stringify(data))
    });
}

function doJSON3() {
    $.getJSON('http://time.jsontest.com/?alloworigin=false', function (data) {
        alert(JSON.stringify(data))
    });
}

jsFiddle

1 个答案:

答案 0 :(得分:0)

示例,请参阅Javascript的var来声明变量。

HTML

<h3>Get JSON with padding</h3>

<button id="ex1">Get JSON</button>
<br>
<br>
<hr>

<h3>Get JSON with Access-Control-Allow-Origin header: *</h3>

<button id="ex2">Get JSON</button>
<br>
<br>
<hr>

<h3>Get JSON with NO Access-Control-Allow-Origin header</h3>

<button id="ex3">Get JSON</button>

的Javascript

var data1,
    data2,
    data3;

function doJSON1() {
    $.getJSON('http://time.jsontest.com/?alloworigin=false&callback=?', function (data) {
        data1 = data;
        console.log(data1);
    });
}

function doJSON2() {
    $.getJSON('http://time.jsontest.com/?alloworigin=true', function (data) {
        data2 = data;
        console.log(data3);
    });
}

function doJSON3() {
    $.getJSON('http://time.jsontest.com/?alloworigin=false', function (data) {
        data3 = data;
        console.log(data3);
    });
}

$('#ex1').on('click', doJSON1);
$('#ex2').on('click', doJSON2);
$('#ex3').on('click', doJSON3);

jsFiddle