将JSON字符串(Pretified)转换回原始字符串(包含所有转义符)
Convert JSON String(Prettified) back to raw string(with all the escape characters)
所以我有一个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}
我需要将其转换为原始字符串格式,在该格式中,所有特殊字符和符号都将替换为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}
我将如何在javascript中做到这一点
欢迎所有的建议。
示例链接http://api.icflix.com/tv/catalogue/movies?num=1
试试这个:
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);
结果:
{"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}
或者这个:
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);
相同结果:
{"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}
该代码将32以下和126以上的字符视为特殊字符。你需要为你的场景更新它。
在这篇文章的帮助下。
您可以使用JSON.parse()
解析字符串(如果您还没有将其作为对象),然后递归地运行它的所有属性。对于既不是对象也不是数组的属性,请使用encodeURIComponent()
对每个值进行编码。
演示:
这是JSFiddle。
代码:
将obj
作为问题中第一个JSON字符串的解析对象。以下是JSFiddle链接中断时的代码:
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;
}
相关文章:
- 解析字符串包含'"'从mysql检索到JSON
- 当字符串包含 HTML 标记时,JSON 解析失败
- 搜索包含包含所需值的范围的数组
- 用于Cordova应用程序的Javascript SQLite-使用参数搜索,其中字符串包含子字符串大小写insenst
- Regex-仅当字符串包含任何其他字符时才允许使用某些字符
- javascript正则表达式匹配字符串包含阿拉伯特殊字符符号'œ;u0640
- 如何将字符串(包含位置)转换为数组
- 字符串包含 ;.
- Javascript 如果字符串包含空格,则用破折号替换空格
- 正则表达式(如果字符串包含 E 或 F 字母)
- JavaScript 搜索字符串包含
- AngularJS - Grunt:是否存在一种解决方案来构建仅包含所需模块的正在运行的应用程序
- 如何获取仅包含所选复选框中的值的数组
- 如何删除包含所有空值的数组列
- 正则表达式,如果字符串包含 a>b 或 a=b 或 a<=b 或 a!=b 或 a==b 中的任何一个,则
- JQUERY 如果字符串包含两个单词
- 如何使 Internet Explorer 在将 innerHTML 传输到
- PHP 字符串包含打印 Javascript 时的换行符问题
- 将JSON字符串(Pretified)转换回原始字符串(包含所有转义符)
- 如果请求的字符串包含引号,为什么encodeURIComponent不工作