1. Serialize to Json string
Use serialization when you need to convert objects to { "street": "科技园路.", "city": "江苏苏州", "country": "中国"}
this format (the way that does not introduce external dlls)
public static String SerializeObject(Object o)
{
System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
String jsonString = json.Serialize(o);
return jsonString;
}
Before using the above method, you have to add the reference "System.Web.Extensions", and then you have to add it in front of the classusing System.Web;
Deserialize to the specified object
JavaScriptSerializer js = new JavaScriptSerializer();
Student model = js.Deserialize< Student>(jsonStr);// //反序列化
Second, connect the string key and value to &
The two methods are similar
. Method 1:
private string dataToString(Dictionary<string,string> data)
{
StringBuilder sub = new StringBuilder();
foreach (string s in data.Keys)
{
if (sub.Length > 0)
{
sub.Append("&");
}
sub.Append(System.Web.HttpUtility.UrlEncode(s) + "=" + System.Web.HttpUtility.UrlEncode(data[s].ToString()));
}
return sub.ToString();
}
Method 2:
private string dataToString(Dictionary<string,string> data)
{
StringBuilder buffer = new StringBuilder();
int i = 0;
foreach (string key in data.Keys)
{
if (i > 0)
{
buffer.AppendFormat("&{0}={1}", key, data[key]);
}
else
{
buffer.AppendFormat("{0}={1}", key, data[key]);
}
i++;
}
return buffer.ToString();
}
operation result