问题
Alright so I am very new to programming and have only learned a little bit of basic 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
Basically what I want to do is retrieve the json from "http://pastebin.com/4SPcTFbQ"(Link 1) and store on of the numbers it returns in some sort of variable. (kind of like using scanf() to retrieve the number and store it to a variable)
From what I have been researching it cannot be done through C and I believe has to be done through javascript. On this site (refer to pastebin Link 2) they provide this example, (refer to pastebin Link 3) but when I try to swap out their example json for the vircurex one it does not seem to work anymore.
Any help at all would be very much appreciated!
Here is the example:
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))
});
}
On jsFiddle
回答1:
Example, see Javascript's var for declaring variables.
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);
On jsFiddle
来源:https://stackoverflow.com/questions/22084319/retrieve-number-via-json-and-store-in-variable