0

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
      }
   }
}
Python2014
  • 151
  • 1
  • 2
  • 6

3 Answers3

1

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.

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

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

ESL
  • 986
  • 11
  • 18
  • I want to extract the data from another website's API and post it on my webpage. Earlier, I wrote the question down wrong :( – Python2014 Apr 10 '14 at 06:52
  • Even if jQuery was available, your code wouldn't work since the data comes from an external URL. – Felix Kling Apr 10 '14 at 06:57
  • I'm not sure which way to use? I only want that one piece of value "networkdiff" to show up on a webpage. – Python2014 Apr 10 '14 at 07:00
  • @Python2014: What problems do you have with my answer? – Felix Kling Apr 10 '14 at 07:06
  • @Python2014 If it's another website you can use the first example, it includes the response as a script. **But** you must add the _callback_ parameter in order to get a script like: callback_function(json_object) – ESL Apr 10 '14 at 07:16
0

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.

Aryan
  • 1,767
  • 2
  • 22
  • 39