繁体   English   中英

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

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

我已经创建了自己的API ,并且想知道如何调用参数? 我一直在尝试使用以下代码调用“ type”参数,但是它是未定义的...即使我仅记录“ json”,控制台中也没有任何显示...

有人可以告诉我我在哪里做错了吗?

非常感谢!!! :)))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> 

查看从服务器获取的部分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"
    },
....

据此,您应该通过以下方式访问第0个元素的“类型”:

json.result[0].type

请注意,接收时您的json会转换为JS对象。 您应该遵循对象的方案来访问它的任何属性。

暂无
暂无

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

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