ASP.NET中Json序列化指定对象的部分属性值
2016-05-05 11:46
615 查看
一、实现代码
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public double Meney { get; set; }
public double Tex { get; set; }
public DateTime Berthday { get; set; }
}
/**//// <summary>
///简单实体 可变属性序列化器
/// </summary>
public class PropertyVariableJsonSerializer
{
readonly System.Web.Script.Serialization.JavaScriptSerializer _serializer = new JavaScriptSerializer();
/**//// <summary>
/// json 序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="propertys"></param>
/// <returns></returns>
public string Serialize<T>(T obj, List<string> propertys)
{
_serializer.RegisterConverters(new[] { new PropertyVariableConveter(typeof(T), propertys) });
return _serializer.Serialize(obj);
}
}
public class PropertyVariableConveter : JavaScriptConverter
{
private readonly List<Type> _supportedTypes = new List<Type>();
public PropertyVariableConveter(Type supportedType, List<string> propertys)
{
_supportedTypes.Add(supportedType);
Propertys = propertys;
}
private List<string> Propertys { get; set; }
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new Exception(" 这个暂时不支持 , 谢谢 ");
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var dic = new Dictionary<string, object>();
var t = obj.GetType();
var properties = t.GetProperties();
foreach (var ite in properties)
{
string key = ite.Name;
var v = t.GetProperty(key).GetValue(obj, null);
if (Propertys == null || Propertys.Count <= 0)
{
dic.Add(key, v);
continue;
}
if (Propertys.Contains(key))
{
dic.Add(key, v);
}
}
return dic;
}
public override IEnumerable<Type> SupportedTypes
{
get { return _supportedTypes; }
}
}
二、调用
文章转载自: ASP.NET中Json序列化指定对象的部分属性值 http://www.studyofnet.com/news/1234.html
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public double Meney { get; set; }
public double Tex { get; set; }
public DateTime Berthday { get; set; }
}
/**//// <summary>
///简单实体 可变属性序列化器
/// </summary>
public class PropertyVariableJsonSerializer
{
readonly System.Web.Script.Serialization.JavaScriptSerializer _serializer = new JavaScriptSerializer();
/**//// <summary>
/// json 序列化
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <param name="propertys"></param>
/// <returns></returns>
public string Serialize<T>(T obj, List<string> propertys)
{
_serializer.RegisterConverters(new[] { new PropertyVariableConveter(typeof(T), propertys) });
return _serializer.Serialize(obj);
}
}
public class PropertyVariableConveter : JavaScriptConverter
{
private readonly List<Type> _supportedTypes = new List<Type>();
public PropertyVariableConveter(Type supportedType, List<string> propertys)
{
_supportedTypes.Add(supportedType);
Propertys = propertys;
}
private List<string> Propertys { get; set; }
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
throw new Exception(" 这个暂时不支持 , 谢谢 ");
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var dic = new Dictionary<string, object>();
var t = obj.GetType();
var properties = t.GetProperties();
foreach (var ite in properties)
{
string key = ite.Name;
var v = t.GetProperty(key).GetValue(obj, null);
if (Propertys == null || Propertys.Count <= 0)
{
dic.Add(key, v);
continue;
}
if (Propertys.Contains(key))
{
dic.Add(key, v);
}
}
return dic;
}
public override IEnumerable<Type> SupportedTypes
{
get { return _supportedTypes; }
}
}
二、调用
public static void aaa() { var p = new Person { Age = 20, Name = "www.studyofnet.com", Meney = 3, Tex = 1}; var s = new PropertyVariableJsonSerializer(); string result = s.Serialize<Person>(p, new List<string>() { "Name", "Age" }); }
文章转载自: ASP.NET中Json序列化指定对象的部分属性值 http://www.studyofnet.com/news/1234.html
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 星外ASP.Net的安全设置相关说明
- C#、ASP.NET通用扩展工具类之TypeParse
- 实现ASP.NET无刷新下载并提示下载完成的开发思路
- C#、ASP.NET通用扩展工具类之LogicSugar
- C#、ASP.NET通用工具类IsWhat?(可以判断数字、身份证、数据类型等等)
- 程序中常用的种代码
- ASP.NET、ASP、PHP、JSP之间有什么区别?
- ASP.NET页面间的传值的几种方法
- 入侵ASP.net网站的经验附利用代码
- 盘点PHP和ASP.NET的10大对比!
- ASP.NET MVC4入门教程(四):添加一个模型
- ASP.NET MVC4入门教程(五):从控制器访问数据模型
- ASP.NET中 CheckBox复选框控件的使用
- ASP.NET中BulletedList列表控件使用及详解
- asp.net OleDbCommand 的用法
- 在ASP.NET 2.0中操作数据之六:编程设置ObjectDataSource的参数值
- ASP.NET MVC4入门教程(二):添加一个控制器
- jquery调用asp.net 页面后台的实现代码
- asp.net中XML如何做增删改查操作