我为将 html 代码更改为 JavaScript 转义所做的是:
string input = "श्रीगंग..."
var output = Regex.Replace(input, @"&#([0-9]*);",
x => String.Format("\\u{0:X4}", int.Parse(x.Groups[1].Value)));
or alternately;
var output = String.Join("", WebUtility.HtmlDecode(input)
.Select(x => "\\u" + ((int)x).ToString("X4")));
但是现在我有一个问题,当我通过 wcf 服务以 json 格式发送这些数据时。我得到了相应的价值。但只有输出变量出错,格式如下:
\\u0026\\u0023\\u0032\\u0033\\u0033\\u0037\\u003B\\u0026\\u0023\\u0032\\u0033\\u0037\\u0035\\u003B\\u0026\\u0023\\ u0032\\u0033\\u0033\\u0038\\u003B\\u0026\\u0023\\u0032\\u0033\\u0036\\u0034\\u003B\\u0020\\u0026\\u0023\\u0032\\u0033\ \u0032\\u0035\\u003B\\u0026\\u0023\\u0032\\u0033\\u0035
我不想要'\\u0026'但只想要'\u0026'作为回应。我已经尝试了所有方法,例如:
output.Replace("\\","'\'");
output.Replace("\\",@"\");
但没有在这里工作。
这是我的 WCF 服务方法:
[OperationContract(Name = "GetByCity")]
[WebInvoke(Method = "GET", UriTemplate = "/GetByCity/DeviceType={DeviceType}&CityId={id}&Limit={limit}", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
NewsList GetByCity(string DeviceType, string id, string limit);
现在我不知道在哪里定义 utf-8 格式。