C# .net 2.0 中Json进行字典排序

有的服务端接口需要对Json进行字典排序,然后再进行相关的验证等,由于服务器上面只能使用.net 2.0,网上查了很多都是.net 2.0以上的,在.net 2.0中无法使用,自己就写了个比较笨的方法,还有就是,Json字符串格式不固定,万一哪天Json多了些字段和多了些其它数组什么的.(下面只是一个实例,没有对是不是数组进行判断)

 

String Json = "{"
                + "\"vCode\": \"cs100\","
                + "\"order\": \"PC1709-000342\","
                + "\"Type\": 1,"
                + "\"number\": \"1\","
                + "\"Name\": \"测试\","
                + "\"arr\": ["
                    + "{"                       
                        + "\"recAddress\": \"这里是卖萌的收件地址1\","
                        + "\"sendaddressee\": \"这里是卖萌的发货地址1\","
                        + "\"billCode\": \"20122311224\","
                        + "\"deliveryCode\": \"20122311224222\","
                    + "},"
                   + "{"                       
                        + "\"recAddress\": \"这里是卖萌的收件地址2\","
                        + "\"sendaddressee\": \"这里是卖萌的发货地址2\","
                        + "\"billCode\": \"20122311223\","
                        + "\"deliveryCode\": \"20122311224221\","
                    + "}"
               + "],"
               + "\"aaaa\": \"这里是测试a\","
               + "\"bbbb\": \"这里是测试b\""
            + "}";

            string temp_a = "";
            //Json转Dictionary
            Dictionary dicJson = Newtonsoft.Json.JsonConvert.DeserializeObject>(Json);
            List> listJson = new List>(dicJson);
            //倒叙排列:只需要把变量s2 和 s1 互换就行了 例: return s1.Value.CompareTo(s2.Value);
            //进行排序 目前是顺序
            listJson.Sort(delegate (KeyValuePair s1, KeyValuePair s2)
            {
                //return s2.Key.CompareTo(s1.Key);//倒序
                //这里可以加一个是否是数组判断[{},{}]
                if (s2.Key.Equals("arr"))
                {
                    if (temp_a.Trim().Length == 0)
                    {
                        temp_a = s2.Value.ToString();
                    }                   
                }
                return s1.Key.CompareTo(s2.Key);//顺序
            });
            dicJson.Clear();//清除dicJson

            //将数据部分再次转换为json字符串
            if (temp_a.Trim().Length > 0)
            {
                string jsondata = Newtonsoft.Json.JsonConvert.SerializeObject(temp_a);
                //获取数据中的  不同类型的数据   
                /*下面是解析JArray的部分*/
                JArray jlist = JArray.Parse(temp_a);
                if (jlist.Count != 0)
                {
                    for (int i = 0; i < jlist.Count; i++)
                    {
                        //TestJson diccddddd = Newtonsoft.Json.JsonConvert.DeserializeObject(jsondata);
                        string temp_jList = jlist[i].ToString();
                        Dictionary dicArry = Newtonsoft.Json.JsonConvert.DeserializeObject>(temp_jList);
                        List> listArry = new List>(dicArry);
                        //倒叙排列:只需要把变量s2 和 s1 互换就行了 例: return s1.Value.CompareTo(s2.Value);
                        //进行排序 目前是顺序
                        listArry.Sort(delegate (KeyValuePair s1, KeyValuePair s2)
                        {
                            return s1.Key.CompareTo(s2.Key);//顺序
                        });
                        dicArry.Clear();//清除dicArry
                    }
                }
            }


            //Dictionary转Json(与上面无关,这里只测试Dictionary转Json)
            Dictionary dic = new Dictionary();
            dic.Add("fga", "aaa");
            dic.Add("sdf", "dd");
            dic.Add("dss", "sd");
            dic.Add("dnline", "sds");
            dic.Add("news", "sd");
            dic.Add("acj", "fd");
            dic.Add("Acj", "dd");
            dic.Add("abc", "18");
            String temp = JsonMapper.ToJson(dic);

 

//key按字母先后顺序
SortedDictionary sorWaybillSystem = new SortedDictionary();
sorWaybillSystem.Add("b", "1234");
sorWaybillSystem.Add("d", "321");
sorWaybillSystem.Add("d", "12321");
sorWaybillSystem.Add("a", "12312");
List> listWaybillSystem = new List>();
//遍历元素
foreach (KeyValuePair kv in sorWaybillSystem)
{
     listWaybillSystem.Add(kv);
}

 

 

 

排序1:

C# .net 2.0 中Json进行字典排序_第1张图片

对数组排序后结果1:

C# .net 2.0 中Json进行字典排序_第2张图片

对数组排序后结果2:

C# .net 2.0 中Json进行字典排序_第3张图片

Dictionary转Json:

C# .net 2.0 中Json进行字典排序_第4张图片

你可能感兴趣的:(C#,Winform)