JSON格式转换JSON.parse(),JSON.stringify()和$.param()

var str = '{"name":"huangxiaojian","age":"23"}'

JSON.parse(str)

结果:

age: "23"

name: "huangxiaojian"

__proto__: Object

------------------------------------------------------------------------------------

var str = '{"name":"huangxiaojian","age":"23"}'

JSON.stringify(str);

结果:

”{"name":"huangxiaojian","age":23}“

如果对应的值为null,则会导致参数消失。

var str = '{"name":"huangxiaojian","age":null}'

JSON.stringify(str);

结果:

”{"name":"huangxiaojian"}“


------------------------------------------------------------------------------------

var str = '{"name":"huangxiaojian","age":"23"}'

$.param(str)

结果:

name=huangxiaojian&age=23



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化精简这段代码// pages/Stores/Stores.js // 请求封装 import request from '../../utils/request' // 高德地图 import amapFile from '../../libs/amap-wx.130' // 地图实例 var myAmapFun Page({ data: { map: [] }, onLoad(options) { console.log(options) const that = this wx.createWACode({ path: '/pages/index/index', width: 430, scene: 'custom_value' }).then(res => { // 在页面中显示生成的小程序码 this.setData({ qrcodeUrl: res.path }); }).catch(err => { console.error(err); }); myAmapFun = new amapFile.AMapWX({ key: '5409c5fd8a9d2c7dfecef1faa8cd3ffc' }); wx.getLocation({ type: 'wgs84', isHighAccuracy: true, success(res) { that.setData({ latitude: res.latitude, longitude: res.longitude, speed: res.speed, accuracy: res.accuracy }) request( '/stores/stores', {}, 'POST').then(res => { that.setData({ map: res.data, }) that.calculateDistance() }) } }) }, // 计算附近门店距离 calculateDistance() { const that = this; const mapVar = JSON.parse(JSON.stringify(that.data.map)); const promises = []; mapVar.forEach(item => { const origin = that.data.longitude + ',' + that.data.latitude; const destination = item.longitude + ',' + item.latitude; const promise = new Promise((resolve, reject) => { myAmapFun.getDrivingRoute({ origin: origin, destination: destination, success: function (data) { item.distance = Math.round(data.paths[0].distance / 100) / 10; resolve(); }, fail: function (err) { reject(err); } }); }); promises.push(promise); }); Promise.all(promises).then(() => { that.setData({ map: mapVar }); console.log(that.data.map) }).catch(err => { console.error(err); }); }, onShareAppMessage() { return { title: '分享标题', path: '/pages/index/index?custom_param=custom_value' }; } })
05-28
下面是对代码的优化和精简: 1. 对于使用频率较高的导入模块,可以将其放在全局变量中,避免在每次使用时都要重新导入。 2. 对于重复使用的变量,可以使用 const 声明变量,避免变量被无意中修改。 3. 对于异步请求,可以使用 async/await 语法糖,让代码更加清晰易读。 4. 对于重复调用的函数,可以将其封装成工具函数,提高代码的复用性。 5. 对于大量的 Promise 链式调用,可以使用 Promise.all() 方法,同时处理多个 Promise 对象,避免产生大量的嵌套。 优化后的代码如下: ```js import request from '../../utils/request' import amapFile from '../../libs/amap-wx.130' const myAmapFun = new amapFile.AMapWX({ key: '5409c5fd8a9d2c7dfecef1faa8cd3ffc' }) Page({ data: { qrcodeUrl: '', map: [] }, async onLoad(options) { const { longitude, latitude } = await this.getLocation() const map = await this.getNearbyStores() this.setData({ map, ...{ longitude, latitude }, qrcodeUrl: await this.createWACode() }) await this.calculateDistance() }, // 获取用户当前位置信息 getLocation() { return new Promise((resolve, reject) => { wx.getLocation({ type: 'wgs84', isHighAccuracy: true, success(res) { resolve({ longitude: res.longitude, latitude: res.latitude }) }, fail(err) { reject(err) } }) }) }, // 获取附近的门店信息 getNearbyStores() { return request('/stores/stores', {}, 'POST').then(res => { return res.data }) }, // 生成小程序码 createWACode() { return wx.createWACode({ path: '/pages/index/index', width: 430, scene: 'custom_value' }).then(res => { return res.path }).catch(err => { console.error(err) }) }, // 计算附近门店距离 calculateDistance() { const { longitude, latitude, map } = this.data const mapVar = JSON.parse(JSON.stringify(map)) const promises = mapVar.map(item => { const origin = `${longitude},${latitude}` const destination = `${item.longitude},${item.latitude}` return new Promise((resolve, reject) => { myAmapFun.getDrivingRoute({ origin, destination, success(data) { item.distance = Math.round(data.paths[0].distance / 100) / 10 resolve() }, fail(err) { reject(err) } }) }) }) return Promise.all(promises).then(() => { this.setData({ map: mapVar }) console.log(this.data.map) }).catch(err => { console.error(err) }) }, onShareAppMessage() { return { title: '分享标题', path: '/pages/index/index?custom_param=custom_value' } } }) ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值