我在c#中使用HTTP RESPONE.WRITE编写Web服务 json回答但问题是当我返回带有包含希伯来语langauge的某个字段的json时,之后json响应未满,结束括号错过..我把代码放在c#和json字符串的响应
json对希伯来语字段的回应
{"Response":"OK","FName":"איגור","LName":"kurylo","User_Name":"0524858214","ErrorMsg":"P
json响应法规与英文字段:
{"Response":"OK","FName":"igor","LName":"kurylo","User_Name":"0524858218","ErrorMsg":"Pass"}
和我在Web服务中的代码 这是json响应的模型
public class Android_Reg
{
public string Response { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public string User_Name { get; set; }
public string ErrorMsg { get; set; }
}
string l = Convert.ToString(jSon.Serialize(reg).ToString().Length);
string jsonr = jSon.Serialize(reg).ToString();
var json = JsonConvert.SerializeObject(reg, Formatting.Indented);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/json";
HttpContext.Current.Response.AddHeader("content-length", l);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Output.Write(jsonr);
谢谢你的帮助:)
修改 好吧,我做一些测试我debuig我的网络服务,我找到了下一个细节 之后
string jsonr = jSon.Serialize(reg).ToString();
这个命令jsonr也用希伯来语和英语字段获得下一个字符串 jsonr
"{\"Response\":\"OK\",\"FName\":\"igor\",\"LName\":\"kurylo\",\"User_Name\":\"0524858281\",\"ErrorMsg\":\"Pass\"}"
"{\"Response\":\"OK\",\"FName\":\"איגור\",\"LName\":\"קורילו\",\"User_Name\":\"0524858270\",\"ErrorMsg\":\"Pass\"}"
也许是因为\字符串被截断,我检查了两个字符串的长度,第一个字符串是92个字符串,第二个字符串是92
答案 0 :(得分:2)
我无法测试这个,但我认为问题在于行HttpContext.Current.Response.AddHeader("content-length", l);
希伯来字符串的长度为5,缺少的字符数为5.要对希伯来语进行编码,它将使用双字节编码,因此缺少5个字符。如果你添加l
希伯来字符的数量,我相信问题就会消失。要测试它,只需使用l + 5
。