Json反序化

 Dictionary<string, object> dictionary = JSONToObject<Dictionary<string, object>>(Str);


public static T JSONToObject<T>(string jsonText)
{
    T local;
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    try
    {
        local = serializer.Deserialize<T>(jsonText);
    }
    catch (Exception exception)
    {
        throw new Exception(exception.Message);
    }
    return local;
}

 

 

public static string ObjectToJSON(object obj)
{
    string str;
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    try
    {
        str = serializer.Serialize(obj);
    }
    catch (Exception exception)
    {
        throw new Exception(emYpxP33xAq8jO8oJ1.eopWKtZX1(0) + exception.Message);
    }
    return str;
}

 

public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
{
    List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
    foreach (DataRow row in dt.Rows)
    {
        Dictionary<string, object> item = new Dictionary<string, object>();
        foreach (DataColumn column in dt.Columns)
        {
            item.Add(column.ColumnName, row[column.ColumnName]);
        }
        list.Add(item);
    }
    return list;
}
 
posted on 2013-01-14 14:30  米高佐敦  阅读(168)  评论(0编辑  收藏  举报