如何返回带有unicode字符的json结果转义为\ u1234

时间:2020-11-30 00:27:24

I'm implementing a method that is returning a json result like:

我正在实现一个返回json结果的方法,如:

public JsonResult MethodName(Guid key){
    var result = ApiHelper.GetData(key); //Data is stored in db as varchar with åäö
    return Json(new { success = true, data = result },"application/json", Encoding.Unicode, JsonRequestBehavior.AllowGet );
}

The displayed result:

显示的结果:

{"success":true,"data":[{"Title":"Here could be characters like åäö","Link":"http://www.url.com/test",...},

But I would like to display it like:

但我想显示它像:

{"success":true,"data":[{"Title":"Here could be characters like \u00e5\u00e4\u00f6","Link":"http:\/\/www.url.com\/test",...},

How can I accomplish this? Can I convert it, parse it or change the responseEncoding in web.config to get it to display unicode characters?

我怎么能做到这一点?我可以转换它,解析它或更改web.config中的responseEncoding以使其显示unicode字符吗?

3 个解决方案

#1


4  

There is no need for escaping if the client is a web browser, or any other client that handles http correctly, as long as your server correctly tells the client about content type and content encoding, and the encoding you select supports the codepoints in the outgoing data.

如果客户端是Web浏览器或任何其他正确处理http的客户端,只要您的服务器正确告诉客户端内容类型和内容编码,并且您选择的编码支持传出中的代码点,则无需转义数据。

If the client does not behave correctly, and it really needs the strings to be escaped like that, you will have to write your own ActionResult class and do the escaping yourself. Inherit from JsonResult to start with, and use reflection to create the JSON document as you like it.

如果客户端行为不正确,并且确实需要像这样转义字符串,则必须编写自己的ActionResult类并自行转义。从JsonResult开始继承,并使用反射来创建JSON文档。

It's a chore!

这是一件苦差事!

EDIT: This will get you started

编辑:这将帮助您入门

public class MyController : Controller {
    public JsonResult MethodName(Guid key){
        var result = ApiHelper.GetData(key);
        return new EscapedJsonResult(result);
    }
}

public class EscapedJsonResult<T> : JsonResult {
    public EscapedJsonResult(T data) {
        this.Data = data;
        this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    }

    public override ExecuteResult(ControllerContext context) {
        var response = context.HttpContext.Response;

        response.ContentType = "application/json";
        response.ContentEncoding = Encoding.UTF8;

        var output = new StreamWriter(response.OutputStream);

        // TODO: Do some reflection magic to go through all properties
        // of the this.Data property and write JSON to the output stream
        // using the StreamWriter

        // You need to handle arrays, objects, possibly dictionaries, numbers,
        // booleans, dates and strings, and possibly some other stuff... All
        // by your self!
    }

    // Finds non-ascii and double quotes
    private static Regex nonAsciiCodepoints =
        new Regex(@"[""]|[^\x20-\x7f]");

    // Call this for encoding string values
    private static string encodeStringValue(string value) {
        return nonAsciiCodepoints.Replace(value, encodeSingleChar);
    }

    // Encodes a single character - gets called by Regex.Replace
    private static string encodeSingleChar(Match match) {
        return "\\u" + char.ConvertToUtf32(match.Value, 0).ToString("x4");
    }
}

#2


0  

There are some ways of escaping, but none of them do exactly what you want (HtmlEncode and UrlEncode) You'll need a user-defined function to do such escape.

有一些转义的方法,但没有一个完全符合你的要求(HtmlEncode和UrlEncode)你需要一个用户定义的函数来做这样的转义。

#3


0  

Not sure if this helps but I was getting unicode characters by using below lines: System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); return Json(jsonSerializer.Serialize(validationResult));

不确定这是否有帮助,但我通过使用以下行获取unicode字符:System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); return Json(jsonSerializer.Serialize(validationResult));

#1


4  

There is no need for escaping if the client is a web browser, or any other client that handles http correctly, as long as your server correctly tells the client about content type and content encoding, and the encoding you select supports the codepoints in the outgoing data.

如果客户端是Web浏览器或任何其他正确处理http的客户端,只要您的服务器正确告诉客户端内容类型和内容编码,并且您选择的编码支持传出中的代码点,则无需转义数据。

If the client does not behave correctly, and it really needs the strings to be escaped like that, you will have to write your own ActionResult class and do the escaping yourself. Inherit from JsonResult to start with, and use reflection to create the JSON document as you like it.

如果客户端行为不正确,并且确实需要像这样转义字符串,则必须编写自己的ActionResult类并自行转义。从JsonResult开始继承,并使用反射来创建JSON文档。

It's a chore!

这是一件苦差事!

EDIT: This will get you started

编辑:这将帮助您入门

public class MyController : Controller {
    public JsonResult MethodName(Guid key){
        var result = ApiHelper.GetData(key);
        return new EscapedJsonResult(result);
    }
}

public class EscapedJsonResult<T> : JsonResult {
    public EscapedJsonResult(T data) {
        this.Data = data;
        this.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
    }

    public override ExecuteResult(ControllerContext context) {
        var response = context.HttpContext.Response;

        response.ContentType = "application/json";
        response.ContentEncoding = Encoding.UTF8;

        var output = new StreamWriter(response.OutputStream);

        // TODO: Do some reflection magic to go through all properties
        // of the this.Data property and write JSON to the output stream
        // using the StreamWriter

        // You need to handle arrays, objects, possibly dictionaries, numbers,
        // booleans, dates and strings, and possibly some other stuff... All
        // by your self!
    }

    // Finds non-ascii and double quotes
    private static Regex nonAsciiCodepoints =
        new Regex(@"[""]|[^\x20-\x7f]");

    // Call this for encoding string values
    private static string encodeStringValue(string value) {
        return nonAsciiCodepoints.Replace(value, encodeSingleChar);
    }

    // Encodes a single character - gets called by Regex.Replace
    private static string encodeSingleChar(Match match) {
        return "\\u" + char.ConvertToUtf32(match.Value, 0).ToString("x4");
    }
}

#2


0  

There are some ways of escaping, but none of them do exactly what you want (HtmlEncode and UrlEncode) You'll need a user-defined function to do such escape.

有一些转义的方法,但没有一个完全符合你的要求(HtmlEncode和UrlEncode)你需要一个用户定义的函数来做这样的转义。

#3


0  

Not sure if this helps but I was getting unicode characters by using below lines: System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); return Json(jsonSerializer.Serialize(validationResult));

不确定这是否有帮助,但我通过使用以下行获取unicode字符:System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer = new System.Web.Script.Serialization.JavaScriptSerializer(); return Json(jsonSerializer.Serialize(validationResult));