亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

微信小程序通過(guò)api接口將json數(shù)據(jù)展現(xiàn)到小程序示例

 更新時(shí)間:2017年01月20日 10:56:27   作者:rorntuck7  
這篇文章主要介紹了微信小程序通過(guò)api接口將json數(shù)據(jù)展現(xiàn)到小程序示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

實(shí)現(xiàn)知乎客戶(hù)端的一個(gè)重要知識(shí)前提就是,要知道怎么通過(guò)知乎新聞的接口,來(lái)把數(shù)據(jù)展示到微信小程序端上。

那么我們這一就先學(xué)習(xí)一下,如何將接口獲取到的數(shù)據(jù)展示到微信小程序上。

1.用到的知識(shí)點(diǎn)

<1> wx.request 請(qǐng)求接口資源(微信小程序api中的發(fā)起請(qǐng)求部分)

<2>swiper 實(shí)現(xiàn)輪播圖的組件

<3>wx:for 循環(huán)語(yǔ)句

<4>微信小程序的基礎(chǔ)知識(shí)

2.實(shí)現(xiàn)原理

首先,先看一下這個(gè)請(qǐng)求函數(shù)

wx.request({
 url: '******', //這里填寫(xiě)你的接口路徑
 header: { //這里寫(xiě)你借口返回的數(shù)據(jù)是什么類(lèi)型,這里就體現(xiàn)了微信小程序的強(qiáng)大,直接給你解析數(shù)據(jù),再也不用去尋找各種方法去解析json,xml等數(shù)據(jù)了
  'Content-Type': 'application/json'
 },
 data: {//這里寫(xiě)你要請(qǐng)求的參數(shù)
  x: '' ,
  y: ''
 },

 success: function(res) {
 //這里就是請(qǐng)求成功后,進(jìn)行一些函數(shù)操作
 console.log(res.data)
 }
})

3.代碼

分解圖

<1>首先上一段知乎接口數(shù)據(jù)的json格式中的開(kāi)頭

 "date":"20161114",
 "stories":[
  {
   "images":[
    "http://jb51.net.com/76125c357aa7b0ca6c9cbc41b4a5326d.jpg"
   ],
   "type":0,
   "id":8975316,
   "ga_prefix":"111422",
   "title":"小事 · 我和你們一樣"
  },
  {
   "images":[
    "http://jb51.net/7c908a5940384123fd88287dbc6a2c98.jpg"
   ],
   "type":0,
   "id":8977438,
   "ga_prefix":"111421",
   "title":"成長(zhǎng)嘛,誰(shuí)說(shuō)就意味著一定要長(zhǎng)大了?"
  },

<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: 'http://news-at.zhihu.com/api/4/news/latest',
  headers: {
  'Content-Type': 'application/json'
  },
  success: function (res) {
  //將獲取到的json數(shù)據(jù),存在名字叫zhihu的這個(gè)數(shù)組中
   that.setData({
   zhihu: res.data.stories,
   //res代表success函數(shù)的事件對(duì),data是固定的,stories是是上面json數(shù)據(jù)中stories

   })
  }
 })


 }
})

<3> index.wxml中

<view >
<swiper indicator-dots="{{indicatorDots}}"
 autoplay="{{autoplay}}" class="banners" interval="{{interval}}" duration="{{duration}}">//這里邊的屬性不重要,看下邊
 <block wx:for="{{zhihu}}">
  <swiper-item class="banner" >
   <image src="{{item.image}}" data-id="{{item.b}}" bindtap="bindViewTap" class="banner-image" width="100%" height="100%"/>
   <text class="banner-title">{{item.title}}</text>
  </swiper-item>
 </block>
</swiper>

</view>

看完這個(gè)代碼,你會(huì)想,根據(jù)微信小程序的綁定原理,這里邊的代碼哪里調(diào)用了onLoad()這個(gè)函數(shù),不用多想,微信小程序給你省略了這些步驟。直接調(diào)用zhihu這個(gè)數(shù)組就行。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論