Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何返回转义为\u1234的unicode字符的json结果_C#_Asp.net Mvc_Json_Unicode - Fatal编程技术网

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

C# 如何返回转义为\u1234的unicode字符的json结果,c#,asp.net-mvc,json,unicode,C#,Asp.net Mvc,Json,Unicode,我正在实现一个返回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, JsonRequestB

我正在实现一个返回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 );
}
显示的结果如下:

{"success":true,"data":[{"Title":"Here could be characters like åäö","Link":"http://www.url.com/test",...},
但我想展示一下:

{"success":true,"data":[{"Title":"Here could be characters like \u00e5\u00e4\u00f6","Link":"http:\/\/www.url.com\/test",...},
我怎样才能做到这一点?
我可以转换它、解析它或更改web.config中的响应编码以使其显示unicode字符吗?

有一些转义方法,但它们都不能完全满足您的需要。您需要一个用户定义的函数来执行此类转义。

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

如果客户端的行为不正确,并且它确实需要像那样转义字符串,那么您必须编写自己的ActionResult类并自己进行转义。首先从JsonResult继承,然后使用反射创建您喜欢的JSON文档

这是件苦差事

编辑:这将让你开始

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");
    }
}

不确定这是否有帮助,但我使用以下行获取unicode字符: System.Web.Script.Serialization.JavaScriptSerializer jsonSerializer=新System.Web.Script.Serialization.JavaScriptSerializer;
返回JsonjsonSerializer.SerializevalidationResult

您的意思是写\u转义而不是UTF-8?UTF-8是一种有效的Unicode编码,您的浏览器可以很好地理解它。为什么您需要\u Escape?第三家为我们开发iphone应用程序的公司表示,他们必须使用这种格式。我不知道为什么,但它与X代码和钛有关。该客户端是另一家公司用钛合金开发的iphone应用程序。字符串和特殊字符的实际转义在C中是什么样子的?替换不起作用?不,如果你使用reflector或类似工具查看HtmlEncode或AntiXss库,那么我想你会看到他们一次扫描一个字符的输入结构,然后输出到StringBuilder。我更新了我的答案,让你从正确的方向开始。你可以使用它来完成所有这些-查看MVC的…除了手头的问题。