您当前的位置: 首页 > 知识百科 > 微信小程序调用api之通过api接口将json数据展现到小程序上

微信小程序调用api之通过api接口将json数据展现到小程序上

时间:2023-07-01 14:05 阅读数:19 人阅读 分类:知识百科

  微信小程序调用api之通过api接口将json数据展现到小程序上是如何实现的呢?下面小编就来带着大家一起来看一看微信小程序如何通过api接口将json数据展现到小程序上。

  <1>首先上一段知乎接口数据的json格式中的开头

  "date":"20161114",

  "stories":[

  {

  "images":[

  "pic2.zhimg/76125c357aa7b0ca6c9cbc41b4a5326d.jpg"

  ],

  "type":0,

  "id":8975316,

  "ga_prefix":"111422",

  "title":"小事 · 我和你们一样"

  },

  {

  "images":[

  "pic1.zhimg/7c908a5940384123fd88287dbc6a2c98.jpg"

  ],

  "type":0,

  "id":8977438,

  "ga_prefix":"111421",

  "title":"成长嘛,谁说就意味着一定要长大了?"

  },12345678910111213141516171819201234567891011121314151617181920

  <2>index.js中

  Page({

  data: {

  duration: 2000,

  indicatorDots: true,

  autoplay: true,

  interval: 3000,

  loading: false,

  plain: false

  },

  onLoad: function () {

  var that = this//不要漏了这句,很重要

  wx.request({

  url: 'news-at.zhihu/api/4/news/latest',

  headers: {

  'Content-Type': 'application/json'

  },

  success: function (res) {

  //将获取到的json数据,存在名字叫zhihu的这个数组中

  that.setData({

  zhihu: res.data.stories,

  //res代表success函数的事件对,data是固定的,stories是是上面json数据中stories

  })

  }

  })

  }

  })

  <3> index.wxml中

  autoplay="{{autoplay}}" class="banners" interval="{{interval}}" duration="{{duration}}">//这里边的属*不重要,看下边

  {{item.title}}

  看完这个代码,你会想,根据微信小程序的绑定原理,这里边的代码哪里调用了onLoad()这个函数,不用多想,微信小程序给你省略了这些步骤。直接调用zhihu这个数组就行。

  以上就是小编为大家整理的关于微信小程序调用api之通过api接口将json数据展现到小程序上的内容。更多更精彩的内容请持续关注小程序商店。

  相关阅读:

       微信小程序接口调入形式

       微信小程序标题栏怎么设置?

       微信小程序怎么设置页面标题?