网站导航素材下载,大连网站seo,福州网站关键排名,免费seo诊断title: 对象转化成base64,再转回对象 date: 2024-08-01 17:54:02 tags: vue3
对象转为base64
/** 将本地对象转为base64 */
function toBase(str) {// 将对象转换为JSON字符串const jsonString JSON.stringify(str);// 使用encodeURIComponent将JSON字符串转换为UTF-8的百分…
title: 对象转化成base64,再转回对象 date: 2024-08-01 17:54:02 tags: vue3
对象转为base64
/** 将本地对象转为base64 */
function toBase(str) {// 将对象转换为JSON字符串const jsonString JSON.stringify(str);// 使用encodeURIComponent将JSON字符串转换为UTF-8的百分比编码形式const encodedString encodeURIComponent(jsonString);// 使用btoa将百分比编码形式的字符串转换为Base64const base64String btoa(encodedString);return base64String;
}base64转回对象
/** 将base64转成本地对象 */
function changeBase(base64String) {// 使用atob将Base64字符串解码为百分比编码形式的字符串const encodedString atob(base64String);// 使用decodeURIComponent将百分比编码形式的字符串转换回JSON字符串const jsonString decodeURIComponent(encodedString);// 将JSON字符串解析回对象const obj JSON.parse(jsonString);// 将数字属性转换回数字类型obj.classScore obj.classScore null ? null : Number(obj.classScore);obj.homeworkScore obj.homeworkScore null ? null : Number(obj.homeworkScore);obj.examScore obj.examScore null ? null : Number(obj.examScore);return obj;
}