放弃使用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
}
}
转载于:https://www.cnblogs.com/hongjiumu/archive/2012/08/30/2664125.html
相关资源:json字符串转c#代码