title: 对象转化成base64,再转回对象
date: 2024-08-01 17:54:02
tags: vue3
对象转为base64
function toBase(str) {
const jsonString = JSON.stringify(str);
const encodedString = encodeURIComponent(jsonString);
const base64String = btoa(encodedString);
return base64String;
}
base64转回对象
function changeBase(base64String) {
const encodedString = atob(base64String);
const jsonString = decodeURIComponent(encodedString);
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;
}