前序:在获取网页内容后,网页返回的数据是Json格式的,我们需要提取其中的数值,并作为字符串显示在界面上面。结果截图如下:
直接贴代码吧:
onLoad: function () {
var that = this
console.log(“开始了”)
wx.login({
success(res) {
var JSCODE=res.code
var chx = ‘https://www.qma2.top/xiaochengxu.aspx?appid=’ + app.APPID + ‘&secret=’ + app.SECRET + ‘&js_code=’ + JSCODE + ‘&grant_type=authorization_code’
console.log(chx)
wx.request({
url: chx,
// data: {
// x: ‘’,
// y: ‘’
// },
header: {
‘content-type’: ‘application/json’ // 默认值
},
success(res) {
console.log(res)
//console.log(res.data)
//console.log(res.data.code)
var ch11=""
for (var item in res.data) {
console.log(‘数值=’ + res.data[item])
console.log(‘关键字=’ +item)
if(ch11=="") {
ch11=“关键字:” + item + “\n”
ch11=ch11 +“数值:” +res.data[item]
}else{
ch11 = ch11 + “\n”+ “关键字:” + item + “\n”
ch11 = ch11 + “数值:” + res.data[item]
}
}
//另外 你也可以直接提取 对应的Json关键字 对应的数值内容
//第 1 种写法
//NeiRong = res.data[“openid”]
//第 2 种写法
//NeiRong=res.data.openid
NeiRong=ch11
console.log(“test:”, NeiRong)
that.setData({
motto: NeiRong
})
},
fail(res) {
console.log(“失败:”, res)
}
})
}})
嗯嗯,这里就结束了。有用的小童鞋门 给个赞吧