简体   繁体   English

使用Javascript从JSON API提取数据

[英]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. 我只想在此API中的“ networkdiff”之后打印出值编号。

This is the URL for the API from a different website: 这是来自其他网站的API的URL:

http://21.luckyminers.com/index.php?page=api&action=getpoolstatus&api_key=8dba7050f9fea1e6a554bbcf4c3de5096795b253b45525c53562b72938771c41 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. 我希望代码自动从上面的URL检索数据,并在“ networkdiff”之后显示值以显示在其他网页上。

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
      }
   }
}

Since the data is coming from an external domain, you can't use Ajax to get the data, unless the server enabled CORS. 由于数据来自外部域,因此除非服务器启用了CORS,否则无法使用Ajax来获取数据。 This doesn't seem to be the case, but it seems to support JSONP : 似乎并非如此,但似乎支持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 . callback=parseResponse使服务器返回JS,其中包括对processResponse的函数调用。 How to access the information you actually want is explained in Access / process (nested) objects, arrays or JSON . 访问/处理(嵌套)对象,数组或JSON中说明了如何访问您真正想要的信息。

In which way you call the JSON? 您以哪种方式调用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): 如果使用JQuery调用它并获取JSON对象,则可以像下面的示例一样在参数中定义回调, 但是必须具有相同的来源 (您的脚本必须在相同的域中运行(在这种情况下为21.luckyminers.com) ):

$.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? 您从哪里获得API密钥? 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 如果将其放在客户端脚本(如JavaScript)上,则任何人都可以读取该密钥,并且使用该密钥可能会造成一些损害……:S

You need to include JSON.js in your web page to use JSON function in javascript. 您需要在网页中包含JSON.js才能在javascript中使用JSON函数。 Here is the URL for download https://github.com/douglascrockford/JSON-js 这是下载的URL https://github.com/douglascrockford/JSON-js

And then you can use beloe code to parse the JOSN string into javascript object. 然后,您可以使用beloe代码将JOSN字符串解析为javascript对象。

var objectJSON = JSON.parse(jsonStr);

You can alse used stringify fucntion to the viceversa. 反之,您还可以使用字符串化功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM