Javascript 将json字符串转义为十六进制表示法

Javascript 将json字符串转义为十六进制表示法,javascript,json,google-apps-script,Javascript,Json,Google Apps Script,我使用Google apps脚本属性服务存储一个JSON对象,并希望使用模板化html服务将该对象作为十六进制转义JSON字符串发送到客户端页面 在服务器端 var str= '{ "test": "hello" }'; //how do I send a hex escaped string var test = JSON.parse(str); console.dir(test); 客户端页中应为字符串 也就是说,客户端中的str对象(查看html源代码)应该读为 var str= "{

我使用Google apps脚本属性服务存储一个JSON对象,并希望使用模板化html服务将该对象作为十六进制转义JSON字符串发送到客户端页面

在服务器端

var str= '{ "test": "hello" }'; //how do I send a hex escaped string 
var test = JSON.parse(str);
console.dir(test);
客户端页中应为字符串 也就是说,客户端中的str对象(查看html源代码)应该读为

var str= "{\x22test\x22: \x22hello\x22}";
var test = JSON.parse(str);
console.dir(test);

如果您只想替换引号,那么可以使用这样一个简单的替换

var str='{“test”:“hello”};
var strEscaped=str.replace(/“/g,\\x22”)

console.dir(strEscaped);
是否只是要十六进制转义的空格?需要这种转义的原因是什么?{test:'he'llo'}将被设置为{\x22test\x22:\x22he'llo\x22},而不会抛出错误,如果值本身有JSON字符,我希望JSON字符转义而不带实际值我更正为var str='{“test”:“hello”" }';,