Python2014
Python2014

Reputation: 151

Extract data from JSON API using Javascript

How do I extract the data below. I only want to print out the value number after "networkdiff" in this API.

This is the URL for the API from a different website:

http://21.luckyminers.com/index.php?page=api&action=getpoolstatus&api_key=8dba7050f9fea1e6a554bbcf4c3de5096795b253b45525c53562b72938771c41

I want the code to automatically retrieve the data from the URL above, and display the value after "networkdiff" to display on my other webpage.

Here's my code so far that I will put in my own webpage:

<HTML>
<body>

<script>
  I don't know what I should put in this script part.
</script>

</body>
</html>

Below is the data the URL showed up as:

{
   "getpoolstatus":{
      "version":"1.0.0",
      "runtime":10.684967041016,
      "data":{
         "pool_name":"21 Coin Pool @ Luckyminers.com",
         "hashrate":0,
         "efficiency":97.79,
         "workers":0,
         "currentnetworkblock":0,
         "nextnetworkblock":1,
         "lastblock":40544,
         "networkdiff":1,
         "esttime":0,
         "estshares":4096,
         "timesincelast":1240429,
         "nethashrate":0
      }
   }
}

Upvotes: 0

Views: 3370

Answers (3)

ESL
ESL

Reputation: 1016

In which way you call the JSON?

You can call it with a callback function (working example), including it as a script:

updateResult=function()
 {
  var s=document.createElement('script');
  s.src=domain+"/index.php?page=api&callback=showResult&action=getpoolstatus&api_key="+api_key;
  document.body.appendChild(s);
 }

You must have the callback defined like:

showResult=function(data)
 {
  document.getElementById('result').innerText=data.getpoolstatus.data.networkdiff;
 }

If you call it with JQuery and get the JSON object, you can define the callback in the argument like the following example, but you must have same-origin (your script must run with the same domain (21.luckyminers.com in this case):

$.getJSON(
  domain+"/index.php?page=api&action=getpoolstatus&api_key="+api_key,
  function(data)
   {
    document.getElementById('result').innerText=data.getpoolstatus.data.networkdiff;
   }
 );

But in any case, be careful. Where did you get the API key? If you put it on a client-side script (like JavaScript) anybody can read the key, and with that key maybe do some damage… :S

Upvotes: 0

Aryan
Aryan

Reputation: 1877

You need to include JSON.js in your web page to use JSON function in javascript. Here is the URL for download https://github.com/douglascrockford/JSON-js

And then you can use beloe code to parse the JOSN string into javascript object.

var objectJSON = JSON.parse(jsonStr);

You can alse used stringify fucntion to the viceversa.

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 816840

Since the data is coming from an external domain, you can't use Ajax to get the data, unless the server enabled CORS. This doesn't seem to be the case, but it seems to support JSONP:

<script>
    function processResponse(data) {
        console.log(data);
    }
</script>
<script src="http://21.luckyminers.com/index.php?page=api&...&callback=processResponse></script>

The callback=parseResponse makes the server return JS consisting of a function call to processResponse. How to access the information you actually want is explained in Access / process (nested) objects, arrays or JSON.

Upvotes: 1

Related Questions