SoFunction
Updated on 2025-03-07

Customize the method to transform Json strings into C# objects


namespace JsonMapper
{
    public class JsonToInstance
    {
        public T ToInstance<T>(string json) where T : new()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
            string[] fields = (',');
            for (int i = 0; i < ; i++ )
            {
                string[] keyvalue = fields[i].Split(':');
                (Filter(keyvalue[0]), Filter(keyvalue[1]));
            }

            PropertyInfo[] properties = typeof(T).GetProperties( | );

            T entity = new T();
            foreach (PropertyInfo property in properties)
            {
                object[] propertyAttrs = (false);
                for (int i = 0; i < ; i++)
                {
                    object propertyAttr = propertyAttrs[i];
                    if (propertyAttr is JsonFieldAttribute)
                    {
                        JsonFieldAttribute jsonFieldAttribute = propertyAttr as JsonFieldAttribute;
                        foreach (KeyValuePair<string ,string> item in dic)
                        {
                            if ( == )
                            {
                                Type t = ;
                                (entity, ToType(t, ), null);
                                break;
                            }
                        }
                    }
                }
            }
            return entity;
        }

        private string Filter(string str)
        {
            if (!(("\"") && ("\"")))
            {
                return str;
            }
            else
            {
                return (1, - 2);
            }
        }

        public object ToType(Type type, string value)
        {
            if (type == typeof(string))
            {
                return value;
            }

            MethodInfo parseMethod = null;

            foreach (MethodInfo mi in (
                | ))
            {
                if ( == "Parse" && ().Length == 1)
                {
                    parseMethod = mi;
                    break;
                }
            }

            if (parseMethod == null)
            {
                throw new ArgumentException((
                    "Type: {0} has not Parse static method!", type));
            }

            return (null, new object[] { value });
        }
    }
}