将JSON字符串(Pretified)转换回原始字符串(包含所有转义符)

Convert JSON String(Prettified) back to raw string(with all the escape characters)

本文关键字:字符串 包含所 转义 原始 转换 JSON Pretified      更新时间:2023-09-26

所以我有一个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;
}
相关文章: