小程序选取上传图片以及视频,JSON.parse解析返回值报错

图片上传后返回的参数
图片上传后返回的参数,正常情况下里面的data值用JSON.parse解析一下就可以拿来用,但这次我遇到报错
在这里插入图片描述
返回的参数看起来没什么问题,然而放这上面一看,发现多了点东西
在这里插入图片描述

下面为解决问题的正确写法
wx.chooseMedia是最新的图片选取写法

//添加图片
addimg(){
     wx.chooseMedia({
         count:9, // 选取数量
         mediaType:['image'],
		 sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
		 success(r) {
             const tempFilePaths = r.tempFiles;
			tempFilePaths.map(item =>{
				wx.uploadFile({
					url:app.baseUrl + '/api/common/upload', 
					filePath: item.tempFilePath,
                     name: 'file',
                     formData: {
                         'user': 'test'
                     },
					success(res){
                         if(res.statusCode==200){
                             //解决问题
                             let content = res.data;
                             if (content[0] != "{") {
                                 content = content.substring(1)
                             }
                             let aa = JSON.parse(content);
                             console.log(aa)
                         }else{
                             wx.showToast({
                                 title:'图片上传失败',
                                 icon:'none'
                             })
                         }
					},
					fail(res){
						wx.showToast({
							title:'图片上传失败',
							icon:'none'
						})
					}
				}) 
             })		
		}
	})
 },

视频选取上传
请添加图片描述
请添加图片描述
请添加图片描述


<view class="checkRight">
    <view class="addImg">
        <view class="stepsEachImg" wx:if="{{houseVideo}}">
            <video 
                src="{{houseVideo.fullurl}}" 
                show-center-play-btn 
                enable-progress-gesture 
                enable-play-gesture 
                style="width: 100%;height: 100%;" 
            ></video>
            <image src="/assets/delete.png" bindtap="deleteVideo" class="delete"></image>
        </view>
        <view class="stepsEachImg" wx:if="{{videoCover && !houseVideo}}">
            <view>
                <progress percent="{{schedule}}" style="width: 180rpx;margin-bottom: 30rpx;" border-radius="4" stroke-width="8" active active-mode="forwards" backgroundColor="#ffffff" activeColor="#09BB07" />
                <view class="addname" style="color: #333;font-size: 30rpx;">上传中...</view>
            </view>
        </view>
        <view class="add" bindtap="choseeVideo" wx:if="{{!houseVideo && !videoCover}}">
            <view>
                <image src="/assets/addimg.png" class="addimg"></image>
                <view class="addname">上传视频</view>
            </view>
        </view>
    </view>
</view>

data:{
	 houseVideo:'',//房屋视频
     videoCover:'',//视频上传时显示进度条
     schedule:0,//进度条进度
}

//上传视频
choseeVideo(){
     const that = this;
     wx.chooseMedia({
         count:1, // 选取数量
         mediaType:['video'],
         sourceType:['album'],//只从相册里选
         success(r) {
             const tempFilePaths = r.tempFiles[0];
             that.setData({
                 videoCover:tempFilePaths
             })
             const uploadTask = wx.uploadFile({
                 url:app.baseUrl + '/api/common/upload', 
                 filePath: tempFilePaths.tempFilePath,
                 name: 'file',
                 formData: {
                     'user': 'test'
                 },
                 header:{
                     token:wx.getStorageSync('token')
                 },
                 success(res){
                     //进度条百分百
                     that.setData({
                         schedule:100
                     })
                     if(res.statusCode==200){
                         //解决问题
                         let content = res.data;
                         if (content[0] != "{") {
                             content = content.substring(1)
                         }
                         let aa = JSON.parse(content);
                         //code码根据自己的来确定
                         if(aa.code == 1){
                             that.setData({
                                 houseVideo:aa.data
                             })
                         }else{
                             that.deleteVideo()
                             wx.showToast({
                                 title:'上传失败,请重试',
                                 icon:'error'
                             })
                         }
                     }else{
                        that.deleteVideo()
                         wx.showToast({
                             title:'上传失败,请重试',
                             icon:'error'
                         })
                     }
                 },
                 fail(res){
                     wx.showToast({
                         title:'视频上传失败',
                         icon:'none'
                     })
                 }
             })
             //监听视频上传,但不太准确
             uploadTask.onProgressUpdate(res =>{
                 if(res.progress > 80){
                     uploadTask.offProgressUpdate();//终止监听
                     return
                 }
                 //进度条数值
                 that.setData({
                     schedule:res.progress
                 })
             })
         }
     })
 },
 //删除视频
 deleteVideo(){
     this.setData({
         houseVideo:'',//房屋视频
         videoCover:'',//视频上传时显示进度条
         schedule:46,//进度条进度
     })
 },
.checkRight{
    width:100%;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    font-size: 32rpx;
}
.addImg{
    width: 100%;
    display: flex;
    align-items: center;
    margin-top: 30rpx;
    flex-wrap: wrap;
}
.add{
    width: 200rpx;
    height: 200rpx;
    border-radius: 10rpx;
    background-color: #eee;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 30rpx;
    margin-bottom: 20rpx;
}
.addimg{
    width: 80rpx;
    height: 80rpx;
}
.addname{
    font-size: 28rpx;
    color: #909090;
}
.stepsEachImg{
    width: 200rpx;
    height: 200rpx;
    border-radius: 10rpx;
    background-color: #eee;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 30rpx;
    margin-bottom: 20rpx;
    position: relative;
}
.stepsImg{
    width: 100%;
    height: 100%;
    border-radius: 10rpx;
}
.delete{
    position: absolute;
    top: -10rpx;
    right: -10rpx;
    width: 40rpx;
    height: 40rpx;
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 当使用JSON.parse解析特殊字符时,可能会报错。为了解决这个问题,可以使用引用\[2\]中提供的handleSpecialCharacters函数来处理特殊字符。该函数会将特殊字符进行转义,然后再进行JSON.parse解析。这样可以避免报错。另外,还可以使用引用\[1\]中提供的方法,将含有特殊字符的字符串进行替换,然后再进行JSON.parse解析。这样也可以解决报错的问题。 #### 引用[.reference_title] - *1* [JSON.parse解析json字符串遇换行符报错 原因以及如何解决](https://blog.csdn.net/m0_66288747/article/details/129931685)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [【前端关于JSON.parse解析报错处理方案】](https://blog.csdn.net/beiluoL/article/details/125085711)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [小程序选取上传图片以及视频JSON.parse解析返回值报错](https://blog.csdn.net/m0_46978096/article/details/125327558)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值