可以使用uni.request()方法向后端发送请求并获取返回的JSON数据,然后使用JSON.parse()方法将字符串转换为JSON对象,最后遍历JSON对象中的每个元素,获取name和url属性的值,并将它们存储在数组中。接下来,你可以使用uni.navigateTo()方法打开对应的url:

<template>
  <view>
    <button @click="getShopList">获取商店列表</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      shopList: []
    }
  },
  methods: {
    getShopList() {
      uni.request({
        url: 'https://web.tvcloud.top/json/shop.json',
        success: (res) => {
          let jsonStr = res.data
          let jsonObj = JSON.parse(jsonStr)
          let list = []
          jsonObj.forEach(item => {
            let shop = {
              name: item.name,
              url: item.url
            }
            list.push(shop)
          })
          this.shopList = list
          this.navigateToUrl()
        },
        fail: (err) => {
          console.log(err)
        }
      })
    },
    navigateToUrl() {
      this.shopList.forEach(item => {
        uni.navigateTo({
          url: item.url
        })
      })
    }
  }
}
</script>

注意:由于uni.navigateTo()方法只能在用户操作时触发,因此在上面的代码示例中,我将navigateToUrl()方法放在getShopList()方法的success回调函数中,以确保在获取完商店列表后再打开对应的url

假设后端httpswebtvcloudtopjsonshopjson的内容为 namerunoob urlwwwrunoobcom namegoogle urlwwwgooglecom name微博 urlwwwweibocom 我想要使用uniapp的script获取到后端对应的name名称后打开对应的url

原文地址: https://www.cveoy.top/t/topic/d76x 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录