About once recording solution Cookie, Json Chinese garbled

Today, the work encountered a problem and needs to put a collection of objects List <Model> stored in a Cookie, according to the original deposit are ok all packaging method, but when the value of all Chinese will become garbled.

First, we can confirm Json and Cookie are likely garbled, we can all join debug code written at the time of conversion Json and write Cookie, so you can see the value into Json and Cookie values.

First solve Json conversion problem, the reason is nothing but garbled coding problems (usually right) Json format conversion so when we need it to be a character encoding :

Encoding encode = System.Text.Encoding.GetEncoding("Unicode");

For the Chinese Json coding can be a normal show ~

 

Name The name of the child or the cookie name or sub-cookie Cookie are not as Chinese, or can not get a cookie, so you can not use Chinese, which are regarded as a way to solve it, but if there is data to be used in the Chinese do, it only a can be encoded as follows:

Deposit Cookie time to encode:

cookie.Value = HttpUtility.UrlEncode("Name");

 

Take cookie time, decode:

cookieValue = HttpUtility.UrlDecode(cookie.Value);

 

 

Finally, the encoding and decoding to be consistent:

System.Web.HttpUtility.UrlDecode

System.Web.HttpUtility.UrlEncode
System.Web.HttpContext.Current.Server.UrlDecode

System.Web.HttpContext.Current.Server.UrlEncode

 

 

Solution:

1. The first set turn into Json string (I Json method using the following conversion when there is no distortion):

string json = ToJson(List<Model>);

 

2. Next we convert good Json string write Cookie:

WriteCookie1("cook", json);

 

This will write a success! ! !

 

3. Next, the data may be removed by the following code:

string jsonStr =GetCookie("cook");//获取json

 

4. Get the collection entity

List <the Model> List ToObject = <List <>> the Model (jsonStr); // JSON converted into a collection of entities

 

The following are accessed Cookie, Json conversion method are:

 

 1  
 2 
 3 /// <summary>
 4 
 5 /// 写cookie值,包括中文编码
 6 
 7 /// </summary>
 8 
 9 /// <param name="strName">名称</param>
10 
11 /// <param name="strValue"></param>
12 
13 public static void WriteCookie(string strName, string strValue)
14 
15 {
16 
17 HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
18 
19 if (cookie == null)
20 
21 {
22 
23 cookie = new HttpCookie(strName);
24 
25 }
26 
27 cookie.Value = HttpUtility.UrlEncode(strValue, Encoding.GetEncoding("UTF-8"));
28 
29 HttpContext.Current.Response.AppendCookie(cookie);
30 
31 }
32 
33 /// <summary>
34 
35 /// 读cookie值,包括中文编码
36 
37 /// </summary>
38 
39 /// <param name="strName">名称</param>
40 
41 /// <returns>cookie值</returns>
42 
43 public static string GetCookie(string strName)
44 
45 {
46 
47 if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null)
48 
49 {
50 
51 return HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies[strName].Value.ToString(), Encoding.GetEncoding("UTF-8"));
52 
53 }
54 
55 return "";
56 
57 }
58 
59 
60 #region Json序列化
61 
62 /// <summary>Json序列化</summary>
63 
64 /// <param name="obj">object </param>
65 
66 /// <returns></returns>
67 
68 public static string ToJson(object obj) {
69 
70 var idtc = newThe DateTimeFormat = {Newtonsoft.Json.Converters.IsoDateTimeConverter " YYYY the MM-dd-HH: mm: SS " };
 71 is  
72   
73 is  
74  return JsonConvert.SerializeObject (obj, IDTC);
 75  
76  }
 77  
78  #endregion 
79  
80   
81  
82  # Region Json deserialization
 83  
84  ///  <Summary> deserialization </ Summary> 
85  
86  ///  <typeParam name = "T"> type </ typeParam> 
87  
88  ///  <param name = "text" > JSON string </ param>
89 
90 /// <returns>类型数据</returns>
91 
92 public static T ToObject<T>(string text) {
93 
94 return (T)JsonConvert.DeserializeObject(text, typeof(T));
95 
96 }

What's wrong with writing a welcome message ~~~~~~

 

Guess you like

Origin www.cnblogs.com/zousc/p/11091035.html