• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

把一个json字符串转换成对应的c#类型

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
放弃使用Newtonsoft,自己动手。
using
System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; using System.Text.RegularExpressions; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { string str = "[{AttributeValueCode:01,AttributeTypeKey:B72CD297-A47E-4081-BA7C-DFFF7BA726D4,AttributeValue:红色,SortOrder:1},{AttributeValueCode:02,AttributeTypeKey:B72CD297-A47E-4081-BA7C-DFFF7BA726D4,AttributeValue:金黄色,SortOrder:3}]"; List<Pro_ProductAttribute> list_ProductAttribute = (new Program()).GetFrontProductAttribueList<Pro_ProductAttribute>(str); Console.WriteLine(str); Console.Read(); } #region 类型转换 private List<T> GetFrontProductAttribueList<T>(string source) where T : new() { List<T> list = new List<T>(); //TODO: T entity; GetMatchChips(source).ForEach(delegate(Dictionary<string, string> item) { entity = SetObejctValue<T>(item); if (entity != null) { list.Add(entity); } }); return list; } private List<Dictionary<string, string>> GetMatchChips(string source) { List<Dictionary<string, string>> dict = new List<Dictionary<string, string>>(); string strSource = source.Substring(1, source.Length - 2); //取得各个对象数据 Regex regex = new Regex(@"({)([^}]+)(})"); MatchCollection matchs = regex.Matches(strSource, 0); Dictionary<string, string> dictTemp; foreach (Match m in matchs) { //TODO dictTemp = GetSpecifiedUnit(m.ToString()); if (dictTemp != null) { dict.Add(dictTemp); } } return dict; } private Dictionary<string, string> GetSpecifiedUnit(string source) { Dictionary<string, string> dict = new Dictionary<string, string>(); Regex regex = new Regex(@"^({)([^}]+)(})"); if (!regex.IsMatch(source, 0)) { return null; } MatchCollection matchs = regex.Matches(source, 0); if (matchs.Count != 1) { return null; } string[] sourceChips = matchs[0].ToString().Substring(1,matchs[0].ToString().Length-2).ToString().Split(','); foreach (string str in sourceChips) { Regex r = new Regex(@"([^:]+)"); if (!r.IsMatch(str, 0)) { throw new ArgumentException("输入的数据含有非法字符!请检查后重新填写产品属性信息!"); } MatchCollection mCollection = r.Matches(str, 0); //if (mCollection.Count != 1) { throw new ArgumentException("输入的数据含有非法字符!请检查后重新填写产品属性信息!"); } //string[] strDictUnit = mCollection[0].ToString().Split(':'); //dict.Add(strDictUnit[0], strDictUnit[1]); dict.Add(mCollection[0].ToString(), mCollection[1].ToString()); } return dict; } struct RetrievePropertyStruct { public string Name; public Type PropertyType; public object Value; } private RetrievePropertyStruct RetrieveProperty(object obj, string propertyName) { RetrievePropertyStruct retrievedDestination; retrievedDestination.Name = propertyName; PropertyInfo propertyInfo = obj.GetType().GetProperty(propertyName); Type propertyType = null; object retrievedObject = null; if (propertyInfo != null) { propertyType = propertyInfo.PropertyType; retrievedObject = propertyInfo.GetValue(obj, null); } else { FieldInfo fieldInfo = obj.GetType().GetField(propertyName); if (fieldInfo != null) { propertyType = fieldInfo.FieldType; retrievedObject = fieldInfo.GetValue(obj); } } retrievedDestination.PropertyType = propertyType; retrievedDestination.Value = retrievedObject; return retrievedDestination; } private T SetObejctValue<T>(Dictionary<string, string> source) where T : new() { T setObject = new T(); PropertyInfo[] propertyInfos = setObject.GetType().GetProperties(); Type propertyType = null; foreach (PropertyInfo propertyInfo in propertyInfos) { propertyType = propertyInfo.PropertyType; IEnumerator<string> enumerator = source.Keys.GetEnumerator(); while (enumerator.MoveNext()) { string current = enumerator.Current; if (current == propertyInfo.Name) { //数据类型转换需要作进一步的统一化就可以通用了 switch (propertyInfo.Name) { case "SortOrder": propertyInfo.SetValue(setObject, Int32.Parse(source[current]), null); break; case "AttributeTypeKey": propertyInfo.SetValue(setObject, new System.Guid(source[current]), null); break; default: propertyInfo.SetValue(setObject, source[current], null); break; } } }//end while(enumerator.MoveNext()) } return setObject; } #endregion } public class Pro_ProductAttribute { #region Private Properties private Guid? _productAttributeKey;//产品属性主键 private Guid? _attributeTypeKey;//属性类型主键 private string _attributeTypeCode;//属性类型编码 private string _attributeTypeName;//属性类型名称 private Guid? _productModelKey;//产品型号主键 private string _productModelCode;//产品型号编码 private string _productModelName;//产品型号名称 private Guid? _attributeValueKey;//属性值主键 private string _attributeValueCode;//属性值编码 private string _attributeValue;//属性值 private int? _sortOrder;//排序号 private int? _weight;//权重 private DateTime? _createTime;//创建时间 private int? _createUser;//创建用户 private DateTime? _updateTime;//最近修改时间 private int? _updateUser;//最近修改用户 private bool? _deleteFlag;//删除标志 private DateTime? _deleteTime;//删除时间 private int? _deleteUser;//删除操作用户 #endregion #region Public Properties /// <summary> /// 产品属性主键. /// </summary> //[ColumnMappingName = "ProductAttributeKey")] public Guid? ProductAttributeKey { get { return _productAttributeKey; } set { object _oldvalue = _productAttributeKey; _productAttributeKey = value; } } /// <summary> /// 属性类型主键. /// </summary> //[ColumnMappingName = "AttributeTypeKey")] public Guid? AttributeTypeKey { get { return _attributeTypeKey; } set { object _oldvalue = _attributeTypeKey; _attributeTypeKey = value; } } /// <summary> /// 属性类型编码. /// </summary> //[ColumnMappingName = "AttributeTypeCode")] public string AttributeTypeCode { get { return _attributeTypeCode; } set { object _oldvalue = _attributeTypeCode; _attributeTypeCode = value; } } /// <summary> /// 属性类型名称. /// </summary> //[ColumnMappingName = "AttributeTypeName")] public string AttributeTypeName { get { return _attributeTypeName; } set { object _oldvalue = _attributeTypeName; _attributeTypeName = value; } } /// <summary> /// 产品型号主键. /// </summary> //[ColumnMappingName = "ProductModelKey")] public Guid? ProductModelKey { get { return _productModelKey; } set { object _oldvalue = _productModelKey; _productModelKey = value; } } /// <summary> /// 产品型号编码. /// </summary> //[ColumnMappingName = "ProductModelCode")] public string ProductModelCode { get { return _productModelCode; } set { object _oldvalue = _productModelCode; _productModelCode = value; } } /// <summary> /// 产品型号名称. /// </summary> //[ColumnMappingName = "ProductModelName")] public string ProductModelName { get { return _productModelName; } set { object _oldvalue = _productModelName; _productModelName = value; } } /// <summary> /// 属性值主键. /// </summary> //[ColumnMappingName = "AttributeValueKey")] public Guid? AttributeValueKey { get { return _attributeValueKey; } set { object _oldvalue = _attributeValueKey; _attributeValueKey = value; } } /// <summary> /// 属性值编码. /// </summary> //[ColumnMappingName = "AttributeValueCode")] public string AttributeValueCode { get { return _attributeValueCode; } set { object _oldvalue = _attributeValueCode; _attributeValueCode = value; } } /// <summary> /// 属性值. /// </summary> //[ColumnMappingName = "AttributeValue")] public string AttributeValue { get { return _attributeValue; } set { object _oldvalue = _attributeValue; _attributeValue = value; } } /// <summary> /// 排序号. /// </summary> //[ColumnMappingName = "SortOrder")] public int? SortOrder { get { return _sortOrder; } set { object _oldvalue = _sortOrder; _sortOrder = value; } } /// <summary> /// 权重. /// </summary> //[ColumnMappingName = "Weight")] public int? Weight { get { return _weight; } set { object _oldvalue = _weight; _weight = value; } } /// <summary> /// 创建时间. /// </summary> //[ColumnMappingName = "CreateTime")] public DateTime? CreateTime { get { return _createTime; } set { object _oldvalue = _createTime; _createTime = value; } } /// <summary> /// 创建用户. /// </summary> //[ColumnMappingName = "CreateUser")] public int? CreateUser { get { return _createUser; } set { object _oldvalue = _createUser; _createUser = value; } } /// <summary> /// 最近修改时间. /// </summary> //[ColumnMappingName = "UpdateTime")] public DateTime? UpdateTime { get { return _updateTime; } set { object _oldvalue = _updateTime; _updateTime = value; } } /// <summary> /// 最近修改用户. /// </summary> //[ColumnMappingName = "UpdateUser")] public int? UpdateUser { get { return _updateUser; } set { object _oldvalue = _updateUser; _updateUser = value; } } /// <summary> /// 删除标志. /// </summary> //[ColumnMappingName = "DeleteFlag")] public bool? DeleteFlag { get { return _deleteFlag; } set { object _oldvalue = _deleteFlag; _deleteFlag = value; } } /// <summary> /// 删除时间. /// </summary> //[ColumnMappingName = "DeleteTime")] public DateTime? DeleteTime { get { return _deleteTime; } set { object _oldvalue = _deleteTime; _deleteTime = value; } } /// <summary> /// 删除操作用户. /// </summary> //[ColumnMappingName = "DeleteUser")] public int? DeleteUser { get { return _deleteUser; } set { object _oldvalue = _deleteUser; _deleteUser = value; } } #endregion } }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#winformtextbox与combox让用户不能输入发布时间:2022-07-10
下一篇:
C#程序不用安装.NET环境运行(让C#程序脱离.net框架)发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap