当前位置: 首页 > 工具软件 > json2html > 使用案例 >

json中含有html代码的处理

边浩波
2023-12-01
//js 解析
function stringToHtml(str) {
    var result = [];
    var list = str.split("-");
    for (var i = 0; i < list.length; i++) {
        var item = list[i];
        if (item.length < 8) {
            result.push(item);
            continue;
        }
        var asciiCode = parseInt(item, 2);
        var charValue = String.fromCharCode(asciiCode);
        result.push(charValue);
    }
    return result.join("");
}
或者
String.prototype.ToHtml = function ToHtml() {
    var result = [];
    var list = this.split("-");
    for (var i = 0; i < list.length; i++) {
        var item = list[i];
        if (item.length < 8) {
            result.push(item);
            continue;
        }
        var asciiCode = parseInt(item, 2);
        var charValue = String.fromCharCode(asciiCode);
        result.push(charValue);
    }
    return result.join("");
}

//C#封装
public static string HtmlToString(string htmlCode)
{
    List<string> dataList = new List<string>();
    for (int i = 0; i < htmlCode.Length; i++) {
        string strChar = htmlCode.Substring(i, 1);
        byte[] data = Encoding.Unicode.GetBytes(strChar);
        if (data.Last() != 0) {//中文
            dataList.Add(strChar);
            dataList.Add("-");
        }
        else
        {
            foreach (byte b in data)
            {
                if (b == 0) continue;
                string itemString = Convert.ToString(b, 2).PadLeft(8, '0');
                dataList.Add(itemString);
            }
            dataList.Add("-");
        }  
    }
    string allDataString = "";
    foreach (string str in dataList) {
        allDataString += str;
    }
    return allDataString;
}

 
 类似资料: