在 JavaScript 中使用两种方式散列 JSON 字符串以用于 URL

2023-09-30前端开发问题
5

本文介绍了在 JavaScript 中使用两种方式散列 JSON 字符串以用于 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想获取一个 JSON 字符串并对其进行加密/散列/编码,以便将其放入 URL 中,使其类似于如下所示的内容:

I would like to take a JSON string and encrypt/hash/encode it so that I can put it into a URL so that it would resemble something such as seen below:

var stringToEncode = JSON.stringify({foo: 'baz', bar: [1,2,3,4,5], baz: {fizzle: 'buzz'}});

'www.myrandomurl.com/someurl/123fas234asf1543rasfsafda'

然后我想获取该加密/散列/编码字符串,并将其解码回其原始 JSON 字符串,以便我可以使用它来绑定到单页 AngularJS 应用程序上的各种元素.

I would then like to take that encrypted/hashed/encoded string, and decode it back to its original JSON string so that I can use it to bind to various elements on a single-page AngularJS application.

JSON 字符串的内容不敏感,因此不需要安全性或复杂的散列.唯一的条件是它必须是一个URL/URI 'safe'"字符串,为了虚荣目的而进行哈希处理,如上所示.

The contents of the JSON string are not sensitive so security or complex hashing is not required. The only condition is that It needs to be a "URL/URI 'safe'" string that is hashed for vanity purposes like seen above.

我对加密/编码的了解有限,但是我考虑过简单地将字符串编码为 Base64 并再次解码.

I am limited in knowledge of encrypting/encoding however I have thought about simply encoding the string to Base64 and decoding it again.

这是最好的方法吗?如果没有,有什么更好的方法?

Is this the best approach? What is a better method if not?

推荐答案

使用 encodeURIComponent() 为 url 编码

Use encodeURIComponent() to encode it for the url

要解码,请使用 decodeURIComponent()函数

To decode use the decodeURIComponent() function

Base64 不是 URL 安全的,因为它可以包含非 url 字符,例如/+ -.(见这个问题)

Base64 is not URL safe since it can contain non url characters like / + -. (See this question)

如果您希望您的 url 与原始字符串不太相似,您可以先转换为 base64,然后通过从 base 64 解码和 covrrtibg 进行编码和反转

If you want your url to not be too similair to the original string you can first covert to base64 and then encode and reverse by decoding and covrrtibg back from base 64

// to url
encodeURIComponent(btoa(str))

// from url
atob(decodeURIComponent(uri))

这篇关于在 JavaScript 中使用两种方式散列 JSON 字符串以用于 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui table数据表格reload where参数保留
1、创建表格对象 layui.use('table', function () { var table = layui.table; tableObj = table.render({ id: "tableId", url: 'url', //数据接口 elem: '#tableId', page: { limit: 15, limits: [15, 30, 50, 100] }, //开启分页 cols: [[ //表头 ... ]], w...
2024-07-18 前端开发问题
385

layui 表格的默认工具栏添加自定义按钮
首先定义table: var tableIns = table.render({ elem:'#businessUserListTable' ,url: ctx+'/business/businessUser/query' ,error:admin.error ,cellMinWidth: 80// ,width:3700 ,toolbar: '#businessUserListTable-toolbar' ,defaultToolbar: [{ title: '...
2024-06-12 前端开发问题
146