C# 键值对 KeyValue 解析

最近看到一个输入字符串或者字节数组解析成键值对的代码,可能对大家有用,简单的写了一下。
当然,你可以用JSON.NET去处理JSON类型的键值对,网上很多资料,就不多说,这里主要说是类似于自定数据格式,类似这样的字符串:

string data = "sdada=57.4,aaasd=1234,fdafdsa=3.2,fdsafdsa=3.515,fdsafd=-3.471,gfdasgfd=0.098,gfdsgf=148.926,测试1=99.999750,测试2=57.1";

当然可以用LINQ来解决。这里用比较传统的方法,直接上代码吧

    /// 
    /// 键值对解析Helper,修改matchKey作为键值之间的符号,matchValue为键值对之间的符号
    /// 
    public static class KeyValueHelper
    {
        static Dictionary<object, object> conents = new Dictionary<object, object>();
        public static string matchKey {get;set;}
        public static string matchValue { get; set; }

        /// 
        /// 解析输入Bytes中的键值对
        /// 
        /// 输入字节数组
        /// 解析后的键值对字典
        public static Dictionary<object, object> GetConentByBytes(byte[] data)
        {
            conents.Clear();
            conents=GetConentByString(Encoding.Default.GetString(data));
            return conents;
        }

        /// 
        /// 解析输入字符串中的键值对
        /// 
        /// 输入字符串
        /// 解析后的键值对字典
        public static Dictionary<object, object> GetConentByString(string data)
        {
            conents.Clear();

            //Predicate matchEqual = delegate(string value)
            //{
            //    return value == "=" ? true : false;
            //};

            //Predicate matchComma = delegate(string value)
            //{
            //    return value == "," ? true : false;
            //};

            if (data.Substring(data.Length - 1) != matchValue)
            {
                data = data + matchValue;
            }

            try
            {
                int pos = 0;
                int startIndex = 0;
                while (true)
                {
                    //Get Key
                    pos = data.IndexOf(matchKey, startIndex);
                    string key = data.Substring(startIndex, pos - startIndex);
                    startIndex = pos + 1;
                    //Get Value
                    pos = data.IndexOf(matchValue, startIndex);
                    string value = data.Substring(startIndex, pos - startIndex);
                    startIndex = pos + 1;
                    conents.Add(key, value);

                    if (startIndex >= data.Length)
                    {
                        break;
                    }
                }
            }
            catch(Exception ex)
            {
                throw new Exception("Error Info: " + ex.ToString());
            }

            return conents;
        }
    }

测试下:

    class Program
    {
        static void Main(string[] args)
        {
            string data = "sdada=57.4,aaasd=1234,fdafdsa=3.2,fdsafdsa=3.515,fdsafd=-3.471,gfdasgfd=0.098,gfdsgf=148.926,测试1=99.999750,测试2=57.1";
            // 本文的测试字符串Key和Value是用“=”连接的,键值之间是“,”隔开的
            KeyValueHelper.matchKey = "=";
            KeyValueHelper.matchValue = ",";
            Dictionary conents = KeyValueHelper.GetConentByString(data);

            Console.WriteLine(data);
            Console.WriteLine("result:");
            foreach (KeyValuePair e in conents)
            {
                Console.WriteLine(string.Format("Key : {0} Value : {1}",e.Key.ToString(),e.Value.ToString()));
            }
            Console.ReadKey();
        } 
  

运行结果:
C# 键值对 KeyValue 解析_第1张图片
收工。

你可能感兴趣的:(桌面应用,乱七八糟)