asp.net – 使用json仅序列化对象的一部分
发布时间:2020-05-23 21:15:41 所属栏目:asp.Net 来源:互联网
导读:我有一个名为MyObject的对象,它有几个属性. MyList是MyObject的列表,我使用linq查询填充,然后将MyList序列化为json.我最终得到这样的东西 ListMyObject MyList = new ListMyObject(); MyList = TheLinqQuery(TheParam); var TheJson = new System.
|
我有一个名为MyObject的对象,它有几个属性. MyList是MyObject的列表,我使用linq查询填充,然后将MyList序列化为json.我最终得到这样的东西 List<MyObject> MyList = new List<MyObject>();
MyList = TheLinqQuery(TheParam);
var TheJson = new System.Web.Script.Serialization.JavaScriptSerializer();
string MyJson = TheJson.Serialize(MyList);
我想要做的是仅序列化MyObject的一部分.例如,我可能有Property1,Property2 …… Propertyn,我希望MyJson只包含Property3,Property5和Property8. 我想到了一种方法,通过创建一个只有我想要的属性的新对象,然后从那里创建一个新的序列化列表.这是最好的方式还是有更好/更快的方式? 谢谢. 解决方法// simple dummy object just showing what "MyObject" could potentially be
public class MyObject
{
public String Property1;
public String Property2;
public String Property3;
public String Property4;
public String Property5;
public String Property6;
}
// custom converter that tells the serializer what to do when it sees one of
// the "MyObject" types. Use our custom method instead of reflection and just
// dumping properties.
public class MyObjectConverter : JavaScriptConverter
{
public override object Deserialize(IDictionary<string,object> dictionary,Type type,JavaScriptSerializer serializer)
{
throw new ApplicationException("Serializable only");
}
public override IDictionary<string,object> Serialize(object obj,JavaScriptSerializer serializer)
{
// create a variable we can push the serailized results to
Dictionary<string,object> result = new Dictionary<string,object>();
// grab the instance of the object
MyObject myobj = obj as MyObject;
if (myobj != null)
{
// only serailize the properties we want
result.Add("Property1",myobj.Property1);
result.Add("Property3",myobj.Property3);
result.Add("Property5",myobj.Property5);
}
// return those results
return result;
}
public override IEnumerable<Type> SupportedTypes
{
// let the serializer know we can accept your "MyObject" type.
get { return new Type[] { typeof(MyObject) }; }
}
}
然后你在哪里序列化: // create an instance of the serializer
JavaScriptSerializer serializer = new JavaScriptSerializer();
// register our new converter so the serializer knows how to handle our custom object
serializer.RegisterConverters(new JavaScriptConverter[] { new MyObjectConverter() });
// and get the results
String result = serializer.Serialize(MyObjectInstance); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何测试ASP.NET会员密码是否符合配置的复杂性要求?
- 启动asp.net输出缓存
- asp.net-mvc – 如何在razor(CSHTML)中设置contenttype?
- ASP.NET MVC认为我的虚拟目录是一个控制器
- asp.net-mvc – 如何调试此错误:’无法找到iisexpress.exe
- 一段时间后的第一个请求总是很慢 – ASP.NET MVC / IIS 8.5
- asp.net – 单个应用程序中的多个母版页
- asp.net-mvc-3 – 在MVC 3的AuthorizeAttribute中获取模型数
- asp.net – 如何在Repeater中为LinkButton做AsyncPostBackT
- asp.net – 加密cookie中的会话ID(或其他身份验证值)是否有
推荐文章
站长推荐
- asp.net – Nlog不创建相对于网站项目的文件
- asp.net – 从Visual Studio 2015发布
- asp.net-mvc-3 – Editor用于收集我的模型中的项
- asp.net – 访问offsetParent时是否存在IE 6/7“
- .net – SqlDataSource与ObjectDataSource
- asp.net-mvc-4 – ASP.Net MVC 4和WebSecurity –
- asp.net-mvc-3 – MVC:路由获取/发布到不同的控
- 为什么在ASP.NET Web API中从请求中删除内容范围
- asp.net-mvc-4 – SimpleMembershipInitializer不
- asp.net-mvc – 有没有人使用史蒂夫·桑德森的Mv
热点阅读
