The following methods are currently conceivable. If there is a better method, please leave a message;
here are two types of escapes
. The first format: the
escape result is:
{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":\"88\",\"isNonProfit\":\"true\"}
The second format with arrays:
escaping the result:
{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":88,\"isNonProfit\":true,\"links\":[{\"strname\":\"Google\",\"strurl\":\"http://www.google.com\"}]}
1. There is only one layer of json data
demo:
public String testZy(String name,String url,String page,String isNonProfit)
{
Dictionary<String, String> dic = new Dictionary<string, string>();
dic.Add("name", name);
dic.Add("url", url);
dic.Add("page", page);
dic.Add("isNonProfit", isNonProfit);
JavaScriptSerializer js = new JavaScriptSerializer();
String data = js.Serialize(dic);
return data;
}
The running result is:
The print result is:
Second, json with array
There are three ways to achieve the effect of escaping
1. Use entity
demo:
public class TestZyData
{
public String name { get; set; }
public String url { get; set; }
public String page { get; set; }
public String isNonProfit { get; set; }
public Links[] links{get;set;}
}
public class Links
{
public String strname;
public String strurl;
}
//此方法的外层类就不体现了
public String arrayZy(String strname,String strurl,String name, String url, String page,String isNonProfit)
{
//使用实体
Links link = new Links
{
strname = strname,
strurl = strurl
};
TestZyData zyData = new TestZyData
{
links = new Links[] { link },
name = name,
url = url,
page = page,
isNonProfit = isNonProfit
};
JavaScriptSerializer js = new JavaScriptSerializer();
String zyStr = js.Serialize(zyData);
System.Diagnostics.Debug.WriteLine("含数组的json字符串转义结果为:" + zyStr);
return zyStr;
}
Escape result:
Note: If you use the first dictionary to escape, the red box will be escaped again, and it will become like this
{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"address\":\[{\\\"street\\\":\\\"科技园路.\\\",\\\"city\\\":\\\"江苏苏州\\\",\\\"country\\\":\\\"中国\\\"}]}
The running result is:
2. Demo in pure dictionary mode :
//使用纯字典模式实现
public String arrayZy2(String strname, String strurl, String name, String url, String page, String isNonProfit)
{
Dictionary<String, Object> dic = new Dictionary<string, Object>();
dic.Add("strname", strname);
dic.Add("strurl", strurl);
Dictionary<String, Object> sdic = new Dictionary<string, Object>();
sdic.Add("name", name);
sdic.Add("url", url);
sdic.Add("page", page);
sdic.Add("isNonProfit", isNonProfit);
Dictionary<String, Object>[] dics = new Dictionary<string, Object>[] { dic };
sdic.Add("links", dics);
JavaScriptSerializer js = new JavaScriptSerializer();
String zyStr = js.Serialize(sdic);
return zyStr;
}
Debugging results:
Running results:
3. Use dictionary and list combination (similar to pure dictionary)
demo:
public String arrayZy3(String strname, String strurl, String name, String url, String page, String isNonProfit)
{
Dictionary<String, Object> dic = new Dictionary<string, Object>();
dic.Add("strname", strname);
dic.Add("strurl", strurl);
Dictionary<String, Object> sdic = new Dictionary<string, Object>();
sdic.Add("name", name);
sdic.Add("url", url);
sdic.Add("page", page);
sdic.Add("isNonProfit", isNonProfit);
List<Dictionary<String, Object>> list = new List<Dictionary<string, Object>>();
list.Add(dic);
sdic.Add("list",list);
JavaScriptSerializer js = new JavaScriptSerializer();
String result = js.Serialize(sdic);
return result;
}
Debugging results:
Running results: