C# escapes data in Json format

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
write picture description here
escape result is:

{\"name\":\"BeJson\",\"url\":\"http://www.bejson.com\",\"page\":\"88\",\"isNonProfit\":\"true\"}

The second format with arrays:
write picture description here
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:
write picture description here

The print result is:
write picture description here

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:
write picture description here
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:
write picture description here


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:
write picture description here
Running results:
write picture description here

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:
write picture description here
Running results:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325535111&siteId=291194637