Web Api 输出Json格式数据,中文要进行Unicode编码(模拟PHP的json_encode输出)
来源:互联网 发布:安易数据恢复软件破解 编辑:程序博客网 时间:2024/06/05 10:53
PHP中的json_encode方法,在5.4之前版本,中文会被unicode编码;5.4加入JSON_UNESCAPED_UNICODE,这个参数,设置不进行escape和unicode处理。
public class DefaultController : ApiController
{public IHttpActionResult GetProduct()
{
Product p =new Product{ Id = 1, Name = "中文"};
var msg = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new JsonContent(p)
};
var response = new ResponseMessageResult(msg);
return response;
}
private string ConvertUnicode(string test)
{
string result = "";
for (int i = 0; i < test.Length; i++)
{
int code = (int)test[i];
if (code > 32 && code < 127)
{
result += test[i];
}
else
result += string.Format("\\u{0:x4}", code);
}
return result;
}
}
public class JsonContent : HttpContent
{
private readonly MemoryStream _Stream = new MemoryStream();
public JsonContent(object value)
{
Headers.ContentType = new MediaTypeHeaderValue("application/json");
var jw = new JsonTextWriter(new StreamWriter(_Stream));
jw.Formatting = Formatting.Indented;
var serializer = new JsonSerializer();
serializer.NullValueHandling = NullValueHandling.Ignore;
serializer.StringEscapeHandling = StringEscapeHandling.EscapeNonAscii;
serializer.Serialize(jw, value);
jw.Flush();
_Stream.Position = 0;
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
return _Stream.CopyToAsync(stream);
}
protected override bool TryComputeLength(out long length)
{
length = _Stream.Length;
return true;
}
}
public class Product
{
public int Id
{
get;
set;
}
public string Name
{
get;
set;
}
public string Desc
{
get;
set;
}
}
0 0
- Web Api 输出Json格式数据,中文要进行Unicode编码(模拟PHP的json_encode输出)
- PHP输出json数据时中文不进行unicode编码的几种方法总结
- php json_encode 格式输出 数组转 json
- php输出json格式数据的例子
- python中json格式数据输出实现方式(无unicode编码输出形式)
- 如何使得中文不被 json_encode 不编码成 unicode PHP 浏览:9722 2014年08月14日 使用 php 自带的 json_encode 函数对数据进行编码时,中文都会变成
- 解决PHP json_encode() 中文自动Unicode编码的函数
- PHP中json_encode中文被Unicode编码的解决办法
- PHP解决json_encode中文变成unicode编码
- php json_encode中文unicode编码问题
- Unity3D通过PHP输出的JSON格式API
- php如何输出中文json格式字符串
- python unicode 编码的中文输出
- 如果JSON数据中有中文,输出Unicode编码,无法识别解决办法
- PHP中json_encode中文显示输出
- PHP输出一组 JSON格式的数据(phpToJson)
- 【json】输出json格式的数据
- 数据输出编码格式
- Spark 实战, 第 2 部分:使用 Kafka 和 Spark Streaming 构建实时数据处理系统
- 常用的python模块及安装方法
- 建立SSH隧道(SSH端口转发)
- 五:音频
- DateTimePicker 控件出现 "You must be in ShowCheckbox mode to set to this date" 的错误
- Web Api 输出Json格式数据,中文要进行Unicode编码(模拟PHP的json_encode输出)
- 智能称体脂称实现(代码与基本数据处理篇)
- Browsersync options
- 微信支付 简单的流程
- EMV处理流程
- C语言数学函数:ceil(), floor(), round()
- MySQL性能优化相关知识
- 传智播客168期JavaEE就业班(第二天 css)
- 由App的启动说起