被坑记

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace StringToObject
{
    public class XmlHelper
    {
        /// <summary>
        /// 将字符串解析为泛型对象List
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strData"></param>
        /// <returns></returns>
        public static List<T> GetObjectListFormString<T>(string strData)
        {
            List<T> output = new List<T>();

            strData = FormatString(strData);
            if (string.IsNullOrEmpty(strData)) return output;

            string[] stringArrar = Regex.Split(strData, "},{", RegexOptions.IgnoreCase);

            List<KeyValueClasss> targetObjectNameList = GetTNameList<T>();

            CompareInfo compare = CultureInfo.InvariantCulture.CompareInfo;

            stringArrar.ToList().ForEach(t =>
            {
                targetObjectNameList.ForEach(l =>
                {
                    l.Index = compare.IndexOf(t, l.Key, CompareOptions.IgnoreCase);
                });
                targetObjectNameList = targetObjectNameList.Where(l => l.Index != -1).OrderBy(l => l.Index).ToList();

                for (int i = 0; i < targetObjectNameList.Count; i++)
                {
                    if (i == 0)
                    {
                        string newValue = string.Empty;
                        if (targetObjectNameList.Count() <= 1)
                        {
                            newValue = t.Substring(targetObjectNameList[i].Key.Length);
                        }
                        else
                        {
                            int index = targetObjectNameList[i].Key.Length;
                            int size = targetObjectNameList[i + 1].Index - targetObjectNameList[i].Index - targetObjectNameList[i + 1].Key.Length - 1;
                            newValue = t.Substring(index, size);
                        }
                        newValue = newValue.Remove(0, 1);
                        newValue = newValue.Remove(newValue.Length - 1, 1);
                        targetObjectNameList[i].Value = newValue;
                    }
                    else if (i == targetObjectNameList.Count() - 1)
                    {
                        int index = targetObjectNameList[i].Index + targetObjectNameList[i].Key.Length;
                        targetObjectNameList[i].Value = t.Substring(index);
                        targetObjectNameList[i].Value = targetObjectNameList[i].Value.Remove(0, 1);
                    }
                    else
                    {
                        int index = targetObjectNameList[i].Index + targetObjectNameList[i].Key.Length;
                        int size = targetObjectNameList[i + 1].Index - targetObjectNameList[i].Index - targetObjectNameList[i].Key.Length;
                        string newValue = t.Substring(index, size);
                        newValue = newValue.Remove(0, 1);
                        newValue = newValue.Remove(newValue.Length - 1, 1);
                        targetObjectNameList[i].Value = newValue;
                    }
                }

                T newObject = System.Activator.CreateInstance<T>();
                newObject.GetType().GetProperties().ToList().ForEach(o =>
                {
                    var objValue = targetObjectNameList.Where(s => s.Key == o.Name).Select(s => s.Value).FirstOrDefault();
                    o.SetValue(newObject, objValue);
                });
                output.Add(newObject);
            });

            return output;
        }
        /// <summary>
        /// 获取对象属性名称列表
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        private static List<KeyValueClasss> GetTNameList<T>()
        {
            var output = new List<KeyValueClasss>();
            var ropertyInfoArray = (typeof(T)).GetProperties();
            ropertyInfoArray.ToList().ForEach(t =>
            {
                output.Add(new KeyValueClasss { Key = t.Name });
            });
            return output;
        }
        /// <summary>
        /// 过滤字符串
        /// </summary>
        /// <param name="strData"></param>
        /// <returns></returns>
        private static string FormatString(string strData)
        {
            if (string.IsNullOrEmpty(strData)) return string.Empty;
            ///过滤空格、换行符
            strData = strData.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
            ///去掉行首2字符 即"[{"
            strData = strData.Substring(2);
            ///去掉行尾2字符 即"}]"
            strData = strData.Substring(0, strData.Length - 2);
            return strData;
        }
    }

    /// <summary>
    /// 键值对类
    /// </summary>
    internal class KeyValueClasss
    {
        public string Key { get; set; }
        public int Index { get; set; }
        public string Value { get; set; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace StringToObject
{
    class Program
    {
        static void Main(string[] args)
        {
            string strData = @"[{material_number=04.04.09.001.0398, material_model=WYD33W04-2200, qty=128, entry_id=frpbjbgpQheLY3CkGASZEiYEHMU=, material_name=浴霸2200W, totalReceiptQty=0}, {material_number=04.04.09.001.0399, material_model=WYD36W05-2200, qty=3332, entry_id=hTuWOEN2SAmasjdHve10syYEHMU=, material_name=浴霸2200W, totalReceiptQty=40}, {material_number=04.04.09.001.0400, material_model=WYD36W05-2400, qty=1000, entry_id=EUqVAulQQDidTA+N1RBQnSYEHMU=, material_name=浴霸2400W, totalReceiptQty=40}, {material_number=04.04.09.001.0401, material_model=WYD36W05-2600, qty=660, entry_id=D/NFnu3dTZGlzO16K6ThDSYEHMU=, material_name=浴霸2600W, totalReceiptQty=40}, {material_number=04.04.09.001.0407, material_model=WYD36W11-2600, qty=200, entry_id=Ut5TcbHCQc602ibG0na7tiYEHMU=, material_name=浴霸2600W, totalReceiptQty=40}, {material_number=04.04.09.001.0404, material_model=WYD36S08-1200, qty=1660, entry_id=YP/F0BgQT1+NA2QXEovgmSYEHMU=, material_name=浴霸1200W, totalReceiptQty=0}, {material_number=04.04.09.001.0405, material_model=WYD33X09-35, qty=300, entry_id=P6BSIaibSLeoNyRwafiBkiYEHMU=, material_name=凉霸35W, totalReceiptQty=0}, {material_number=04.04.09.001.0406, material_model=WYD33X10-40, qty=2000, entry_id=a+Yr0Ep8QWWPha7p36mmnSYEHMU=, material_name=换气扇40W, totalReceiptQty=0}]";
            //strData = @"[{

            //orderCustomerNnumber=01.008.0001, 
            //material_number=01.01.21.050.0579, 
            //material_model=T=0.3锰钢 表面:镀镍 26*10*8.5mm 图号:CMLS-MT5X06-009, 
            //qty=1500, 
            //orderCustomerName=华东零星客户, 
            //entry_id=6GBFaZ78TjikdC/OjvLwA8z9SSM=, 
            //material_name=挂板, 
            //shippedQty=1500

            //}, {orderCustomerNnumber=01.008.0001, material_number=01.02.12.001.0080, material_model=十字沉头4*20AN铁镀锌Rohs, qty=1500, orderCustomerName=华东零星客户, entry_id=E/u2J7tGR7m1zdz/tCKk4cz9SSM=, material_name=螺丝, shippedQty=1500}]";

            //strData = @"[{orderCustomerNnumber=01.008.0001, material_number=01.02.13.070.3045, material_model=10W光面球泡-商超版彩盒-6500K-350G灰底白板,尺寸62*62*115,WA2W21-10-886E-02-SC, qty=3000, orderCustomerName=华东零星客户, entry_id=jOgMCPN/TP2nY170m1XU8sz9SSM=, material_name=内盒, shippedQty=3000}]";

            List<TestClass> ttt = XmlHelper.GetObjectListFormString<TestClass>(strData);

            Console.WriteLine(strData);
            Console.ReadLine();
        }
    }

    public class TestClass
    {
        public string MATerial_number { get; set; }
        public string material_model { get; set; }
        public string qty { get; set; }
        //public string entry_id { get; set; }
        //public string material_name { get; set; }
        ////public string totalReceiptQty { get; set; }

        //public string orderCustomerNnumber { get; set; }
        //public string orderCustomerName { get; set; }
        //public string shippedQty { get; set; }

        //public string sadfsaf { get; set; }
    }
}

猜你喜欢

转载自www.cnblogs.com/eedc/p/9261463.html