简体   繁体   English

如何从html中的API(json)调用参数

[英]How to call parameters from an API (json) in html

I have created my own API , and would like to know how could I call the parameters? 我已经创建了自己的API ,并且想知道如何调用参数? I have been trying to call the "type" parameter with the following code but it's being undefined... And nothing shows up in the console even when I am only logging "json"... 我一直在尝试使用以下代码调用“ type”参数,但是它是未定义的...即使我仅记录“ json”,控制台中也没有任何显示...

Can some one please tell me where did I do wrong please? 有人可以告诉我我在哪里做错了吗?

Thanks a lot!!! 非常感谢!!! :))) xxx :)))xxx

  <canvas id="myCanvas"> </canvas> <script type="text/javascript" src="js/jQuery.js"></script> <script type="text/javascript"> var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.canvas.width = window.innerWidth; ctx.canvas.height = window.innerHeight; $(document).ready(function(){ var ctx = canvas.getContext("2d"); ctx.beginPath(); ctx.fillStyle = "rgb(255,255,255)"; ctx.fillRect(0,0,canvas.width, canvas.height); var url = "https://sheetsu.com/apis/f924526c"; $.getJSON(url,function(json){ console.log(json); var type = json.type; $('type').append(type); console.log(type); }); }); </script> 

Look at a part of the JSON data you got from the server: 查看从服务器获取的部分JSON数据:

{
  "status": 200,
  "success": true,
  "result": [
    {
      "number": "0",
      "URL random digit1": "1",
      "URL random digit2": "8",
      "id": "1a8",
      "name": "user1",
      "type": "mf",
      "url": "http://test.com/customised_url_1a8",
      "message": "bonjour user1"
    },
....

According to this, you should access the 0th element's "type" in the following manner: 据此,您应该通过以下方式访问第0个元素的“类型”:

json.result[0].type

Please note than your json is converted to a JS object upon reception. 请注意,接收时您的json会转换为JS对象。 You should follow the object's scheme to access any property of it. 您应该遵循对象的方案来访问它的任何属性。

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

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