Javascript 将JSON字符串(经过修饰)转换回原始字符串(包含所有转义字符)

Javascript 将JSON字符串(经过修饰)转换回原始字符串(包含所有转义字符),javascript,json,Javascript,Json,所以我有一个json字符串,如下所示: {"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame

所以我有一个json字符串,如下所示:

 {"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http://192.168.195.86:1081/image/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http://192.168.195.86:1081/eng/movie/pkdaci6s-ice-bound","description":"Testin'g «ταБЬℓσ»: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http://192.168.195.86:1081/movie/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http://192.168.195.86:1081/category/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1}   
{“计数”:1,“项目”:[{“评级”:“青少年”,“视频”:[{“宽度”:1280,“链接”:”http://192.168.195.86:1081/playback/play/99e2399e-368b,“dar_图像”:1.777 8,“语言”:“英语”,“持续时间”:5166,“类型”:“主要”,“id”:“99e2399e-368b”,“dar_框架”:1.777 8,“高度”:720}],“试验”:假,“元数据语言”:“英语”,“年份”:2003,“图像”:[{“链接”:"http://192.168.195.86:1081/image/1675610d-e57e.jpg,“类型”:“海报”,“id”:“1675610d-e57e”,“方向”:“景观”}],“id”:“d0e9d382-e2d4”,“有下载政策”:真,“标题”:“冰封”,“章节”:“好莱坞”,“剧组”:[{“角色”:“导演”,“姓名”:“罗杰·斯波提斯伍德”}],“类型”:“电影”,“网站”\uURL:“http://192.168.195.86:1081/eng/movie/pkdaci6s-冰结”,“说明”:“试验«ταБа”ℓσ»:13,现在打八折!@$%^&*()。另一个测试字符串,“链接”:http://192.168.195.86:1081/movie/d0e9d382-e2d4,“slug”:“pkdaci6s”,“categories”:[{“link”:”http://192.168.195.86:1081/category/ca8a9dce-514d,“id”:“ca8a9dce-514d”,“标题”:“戏剧”}],“元数据方向”:“ltr”,“音频语言”:[“英语”],“演员”:[{“姓名”:“苏珊·萨兰登”},{“姓名”:”Aidan Devine“},{“姓名”:“Cynthia Mace”}],“剩余”:1}
我需要将其转换为原始字符串格式,其中所有特殊字符和符号都将替换为unicode或转义字符

 {"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http:\/\/192.168.195.86:1081\/playback\/play\/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http:\/\/192.168.195.86:1081\/image\/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http:\/\/192.168.195.86:1081\/eng\/movie\/pkdaci6s-ice-bound","description":"Testin\'g \u2xxx \u2xxx\u2xxx\u2xxx\u2xxx\u2xxx\u2xxx\u2xxx: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http:\/\/192.168.195.86:1081\/movie\/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http:\/\/192.168.195.86:1081\/category\/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1}   
{“count”:1,“items”:[{“rating”:“teents”,“videos”:[{“width”:1280,“link”:“http:\/\/192.168.195.86:1081\/play\/play\/99e2399e-368b”,“daru image”:1.777777777 8,“language”:“eng”,“duration”:5166,“type”:“main”,“id”:“99e2399e-368b”,“daru frame”:1.77777 8,“height”:720}],“trial”:false,“metadata”;“eng”:“eng”;“year”;“2003年”,图像[“link”http:\/\/192.168.195.86:1081\/image\/1675610d-e57e.jpg,“类型”:“海报”,“id”:“1675610d-e57e”,“方向”:“风景”}],“id”:“d0e9d382-e2d4”,“有下载政策”:真,“标题”:“冰封”,“章节”:“好莱坞”,“剧组”:[{“角色”:“导演”,“姓名”:“罗杰斯波蒂斯伍德”}],“类型”:“电影”,“网站url”:http:\/\/192.168.195.86:1081\/movie\/pkdaci6s冰封,“描述”:“Testin\g\uxxx\uxxx\uxxx\uxxx\uxxx\uxxx\uxxx\uxxx\uxxx\uxxx\uxxx:13,现在打八折!@$%^&*()。另一个测试字符串”,“链接”:“http:\/\/192.168.195.86:1081\/movie\/d0e9d382-e2d4”,“slug”:“pkdaci6s”,“类别”:“链接”:“链接”:“http:\/\/192.168.195“:”ca8a9dce-514d“,”标题“:”戏剧“}],”元数据方向“:”ltr“,”音频语言“:[”英语“,”演员“:[{”姓名“:”苏珊·萨兰登“},{”姓名“:”艾丹·德文“},{”姓名“:”辛西亚·梅斯“}],”剩余“:”1}”
我将如何在javascript中执行此操作?
欢迎大家指点


示例链接 试试这个:

function escapeString(str) {
    var result = '',
        code;

    for (var i = 0; i < str.length; i++) {
        code = str.charCodeAt(i);
        if (code < 32 || code > 126) {
            result += '\\u2' + code;
        } else {
            result += str[i];
        }
    }
    return result;
}

var orig = '{"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http://192.168.195.86:1081/image/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http://192.168.195.86:1081/eng/movie/pkdaci6s-ice-bound","description":"Testin\'g «ταБЬℓσ»: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http://192.168.195.86:1081/movie/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http://192.168.195.86:1081/category/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1} ';

result = escapeString(orig);
console.log(result);
函数转义限制(str){
var结果=“”,
代码;
对于(变量i=0;i126){
结果+='\\u2'+代码;
}否则{
结果+=str[i];
}
}
返回结果;
}
var orig='{“计数”:1,“项目”:[{“评级”:“青少年”,“视频”:[{“宽度”:1280,“链接”:”http://192.168.195.86:1081/playback/play/99e2399e-368b,“dar_图像”:1.777 8,“语言”:“英语”,“持续时间”:5166,“类型”:“主要”,“id”:“99e2399e-368b”,“dar_框架”:1.777 8,“高度”:720}],“试验”:假,“元数据语言”:“英语”,“年份”:2003,“图像”:[{“链接”:"http://192.168.195.86:1081/image/1675610d-e57e.jpg,“类型”:“海报”,“id”:“1675610d-e57e”,“方向”:“景观”}],“id”:“d0e9d382-e2d4”,“有下载政策”:真,“标题”:“冰封”,“章节”:“好莱坞”,“剧组”:[{“角色”:“导演”,“姓名”:“罗杰·斯波提斯伍德”}],“类型”:“电影”,“网站”\uURL:“http://192.168.195.86:1081/eng/movie/pkdaci6s-“冰结”,“说明”:“试验”«ταБаℓσ»:13,现在打八折!@$%^&*()。另一个测试字符串,“链接”:http://192.168.195.86:1081/movie/d0e9d382-e2d4,“slug”:“pkdaci6s”,“categories”:[{“link”:”http://192.168.195.86:1081/category/ca8a9dce-514d,“id”:“ca8a9dce-514d”,“标题”:“戏剧”}],“元数据方向”:“ltr”,“音频语言”:[“英语”],“演员”:[{“姓名”:“苏珊·萨兰登”},{“姓名”:”艾丹·德瓦恩,{“姓名”:“辛西娅·梅斯”}],“剩余”:1}';
结果=逃逸限制(原始);
控制台日志(结果);
结果:

{"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http://192.168.195.86:1081/image/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http://192.168.195.86:1081/eng/movie/pkdaci6s-ice-bound","description":"Testin'g \u2171\u2964\u2945\u21041\u21068\u28467\u2963\u2187: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http://192.168.195.86:1081/movie/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http://192.168.195.86:1081/category/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1} 
{“计数”:1,“项目”:[{“评级”:“青少年”,“视频”:[{“宽度”:1280,“链接”:”http://192.168.195.86:1081/playback/play/99e2399e-368b,“dar_图像”:1.777 8,“语言”:“英语”,“持续时间”:5166,“类型”:“主要”,“id”:“99e2399e-368b”,“dar_框架”:1.777 8,“高度”:720}],“试验”:假,“元数据语言”:“英语”,“年份”:2003,“图像”:[{“链接”:”http://192.168.195.86:1081/image/1675610d-e57e.jpg,“类型”:“海报”,“id”:“1675610d-e57e”,“方向”:“景观”}],“id”:“d0e9d382-e2d4”,“有下载政策”:真,“标题”:“冰封”,“章节”:“好莱坞”,“剧组”:[{“角色”:“导演”,“姓名”:“罗杰·斯波提斯伍德”}],“类型”:“电影”,“网站”\uURL:“http://192.168.195.86:1081/eng/movie/pkdaci6s-冰封,“说明”:“Testin'g\u2171\u2964\u2945\u21041\u21068\u28467\u2963\u2187:13,现在打八折!@$%^&*()。另一个测试字符串”,“链接”:http://192.168.195.86:1081/movie/d0e9d382-e2d4,“slug”:“pkdaci6s”,“categories”:[{“link”:”http://192.168.195.86:1081/category/ca8a9dce-514d,“id”:“ca8a9dce-514d”,“标题”:“戏剧”}],“元数据方向”:“ltr”,“音频语言”:[“英语”],“演员”:[{“姓名”:“苏珊·萨兰登”},{“姓名”:“艾丹·德瓦恩”},{“姓名”:“辛西娅·梅斯”}],“剩余”:1}
或者这个:

result = orig.split('').map(function(v) {
    var code = v.charCodeAt(0);
    if (code < 32 || code > 126) {
        return '\\u2' + code;
    } else {
        return v;
    }
}).join('');

console.log(result);
result=orig.split(“”).map(函数(v){
var代码=v.charCodeAt(0);
如果(代码<32 | |代码>126){
返回“\\u2”+代码;
}否则{
返回v;
}
}).加入(“”);
控制台日志(结果);
同样的结果:

{"count":1,"items":[{"rating":"teens","videos":[{"width":1280,"link":"http://192.168.195.86:1081/playback/play/99e2399e-368b","dar_image":1.7777777778,"language":"eng","duration":5166,"type":"main","id":"99e2399e-368b","dar_frame":1.7777777778,"height":720}],"trial":false,"metadata_language":"eng","year":2003,"images":[{"link":"http://192.168.195.86:1081/image/1675610d-e57e.jpg","type":"poster","id":"1675610d-e57e","orientation":"landscape"}],"id":"d0e9d382-e2d4","has_download_policy":true,"title":"Ice Bound","section":"hollywood","crew":[{"role":"director","name":"Roger Spottiswoode"}],"type":"movie","website_url":"http://192.168.195.86:1081/eng/movie/pkdaci6s-ice-bound","description":"Testin'g \u2171\u2964\u2945\u21041\u21068\u28467\u2963\u2187: 1<{2 & 4+1}>3, now 20% off!@#$%^&*(). Another Testing String ","link":"http://192.168.195.86:1081/movie/d0e9d382-e2d4","slug":"pkdaci6s","categories":[{"link":"http://192.168.195.86:1081/category/ca8a9dce-514d","id":"ca8a9dce-514d","title":"Drama"}],"metadata_direction":"ltr","audio_languages":["eng"],"cast":[{"name":"Susan Sarandon"},{"name":"Aidan Devine"},{"name":"Cynthia Mace"}]}],"remaining":1} 
{“计数”:1,“项目”:[{“评级”:“青少年”,“视频”:[{“宽度”:1280,“链接”:”http://192.168.195.86:1081/playback/play/99e2399e-368b,“dar_图像”:1.777 8,“语言”:“英语”,“持续时间”:5166,“类型”:“主要”,“id”:“99e2399e-368b”,“dar_框架”:1.777 8,“高度”:720}],“试验”:假,“元数据语言”:“英语”,“年份”:2003,“图像”:[{“链接”:“http://1
escapeObject(obj);
var escaped = JSON.stringify(obj);
document.getElementById("result").innerHTML = escaped;

function escapeObject(theObject) {
  var result = null;
  if(theObject instanceof Array) {
    for(var i = 0; i < theObject.length; i++) {
        result = escapeObject(theObject[i]);
        if (result) {
            break;
        }   
    }
  }
  else
  {        
      for(var prop in theObject) {    
        if (typeof theObject[prop] == typeof "") {
          var escValue = encodeURIComponent(theObject[prop]);
          theObject[prop] = escValue;  
        }
        if(theObject[prop] instanceof Object || theObject[prop] instanceof Array) {
            result = escapeObject(theObject[prop]);
            if (result) {
                break;
            }
        }

    };
  }
  return result;
}