C# 关于JavaScriptSerializer的一些问题
当使用JavaScriptSerializer进行序列化时,可以忽略类的某些字段吗C# 关于JavaScriptSerializer的一些问题,c#,.net,json,C#,.net,Json,当使用JavaScriptSerializer进行序列化时,可以忽略类的某些字段吗 当使用JavaScriptSerializer进行序列化时,我们可以更改字段的名称吗? 例如,字段为string,但我希望它映射到isOK 您可以使用跳过属性: using System; using System.Web.Script.Serialization; public class Group { // The JavaScriptSerializer ignores this field.
using System;
using System.Web.Script.Serialization;
public class Group
{
// The JavaScriptSerializer ignores this field.
[ScriptIgnore]
public string Comment;
// The JavaScriptSerializer serializes this field.
public string GroupName;
}
您可以使用跳过属性:
using System;
using System.Web.Script.Serialization;
public class Group
{
// The JavaScriptSerializer ignores this field.
[ScriptIgnore]
public string Comment;
// The JavaScriptSerializer serializes this field.
public string GroupName;
}
为了获得最大的灵活性(因为您也提到了名称),理想的做法是在JavaScriptSerializer
对象上调用RegisterConverters
,注册一个或多个JavaScriptConverter
实现(可能在数组或迭代器块中)
然后,通过向返回的字典中添加键/值对,您可以实现序列化
,以在任何名称下添加(或不添加)和值。如果数据是双向的,您还需要一个匹配的反序列化
,但通常(对于ajax服务器),这不是必需的
完整示例:
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
class Foo
{
public string Name { get; set; }
public bool ImAHappyCamper { get; set; }
private class FooConverter : JavaScriptConverter
{
public override object Deserialize(System.Collections.Generic.IDictionary<string, object> dictionary, System.Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override System.Collections.Generic.IEnumerable<System.Type> SupportedTypes
{
get { yield return typeof(Foo); }
}
public override System.Collections.Generic.IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var data = new Dictionary<string, object>();
Foo foo = (Foo)obj;
if (foo.ImAHappyCamper) data.Add("isOk", foo.ImAHappyCamper);
if(!string.IsNullOrEmpty(foo.Name)) data.Add("name", foo.Name);
return data;
}
}
private static JavaScriptSerializer serializer;
public static JavaScriptSerializer Serializer {
get {
if(serializer == null) {
var tmp = new JavaScriptSerializer();
tmp.RegisterConverters(new [] {new FooConverter()});
serializer = tmp;
}
return serializer;
}
}
}
static class Program {
static void Main()
{
var obj = new Foo { ImAHappyCamper = true, Name = "Fred" };
string s = Foo.Serializer.Serialize(obj);
}
}
使用系统;
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
福班
{
公共字符串名称{get;set;}
公共bool ImAHappyCamper{get;set;}
私有类FooConverter:JavaScriptConverter
{
公共重写对象反序列化(System.Collections.Generic.IDictionary字典、System.Type类型、JavaScriptSerializer序列化程序)
{
抛出新的NotImplementedException();
}
公共覆盖System.Collections.Generic.IEnumerable SupportedTypes
{
获取{yield return typeof(Foo);}
}
公共重写System.Collections.Generic.IDictionary序列化(对象obj,JavaScriptSerializer序列化程序)
{
var data=newdictionary();
Foo-Foo=(Foo)obj;
if(foo.imahappycamp)data.Add(“isOk”,foo.imahappycamp);
如果(!string.IsNullOrEmpty(foo.Name))data.Add(“Name”,foo.Name);
返回数据;
}
}
私有静态JavaScriptSerializer序列化程序;
公共静态JavaScriptSerializer序列化程序{
得到{
if(序列化程序==null){
var tmp=新的JavaScriptSerializer();
RegisterConverters(new[]{newfooconverter()});
序列化程序=tmp;
}
返回序列化程序;
}
}
}
静态类程序{
静态void Main()
{
var obj=newfoo{imahappycamp=true,Name=“Fred”};
字符串s=Foo.Serializer.Serialize(obj);
}
}
为了获得最大的灵活性(因为您也提到了名称),理想的做法是在JavaScriptSerializer
对象上调用RegisterConverters
,注册一个或多个JavaScriptConverter
实现(可能在数组或迭代器块中)
然后,通过向返回的字典中添加键/值对,您可以实现序列化
,以在任何名称下添加(或不添加)和值。如果数据是双向的,您还需要一个匹配的反序列化
,但通常(对于ajax服务器),这不是必需的
完整示例:
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
class Foo
{
public string Name { get; set; }
public bool ImAHappyCamper { get; set; }
private class FooConverter : JavaScriptConverter
{
public override object Deserialize(System.Collections.Generic.IDictionary<string, object> dictionary, System.Type type, JavaScriptSerializer serializer)
{
throw new NotImplementedException();
}
public override System.Collections.Generic.IEnumerable<System.Type> SupportedTypes
{
get { yield return typeof(Foo); }
}
public override System.Collections.Generic.IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
var data = new Dictionary<string, object>();
Foo foo = (Foo)obj;
if (foo.ImAHappyCamper) data.Add("isOk", foo.ImAHappyCamper);
if(!string.IsNullOrEmpty(foo.Name)) data.Add("name", foo.Name);
return data;
}
}
private static JavaScriptSerializer serializer;
public static JavaScriptSerializer Serializer {
get {
if(serializer == null) {
var tmp = new JavaScriptSerializer();
tmp.RegisterConverters(new [] {new FooConverter()});
serializer = tmp;
}
return serializer;
}
}
}
static class Program {
static void Main()
{
var obj = new Foo { ImAHappyCamper = true, Name = "Fred" };
string s = Foo.Serializer.Serialize(obj);
}
}
使用系统;
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
福班
{
公共字符串名称{get;set;}
公共bool ImAHappyCamper{get;set;}
私有类FooConverter:JavaScriptConverter
{
公共重写对象反序列化(System.Collections.Generic.IDictionary字典、System.Type类型、JavaScriptSerializer序列化程序)
{
抛出新的NotImplementedException();
}
公共覆盖System.Collections.Generic.IEnumerable SupportedTypes
{
获取{yield return typeof(Foo);}
}
公共重写System.Collections.Generic.IDictionary序列化(对象obj,JavaScriptSerializer序列化程序)
{
var data=newdictionary();
Foo-Foo=(Foo)obj;
if(foo.imahappycamp)data.Add(“isOk”,foo.imahappycamp);
如果(!string.IsNullOrEmpty(foo.Name))data.Add(“Name”,foo.Name);
返回数据;
}
}
私有静态JavaScriptSerializer序列化程序;
公共静态JavaScriptSerializer序列化程序{
得到{
if(序列化程序==null){
var tmp=新的JavaScriptSerializer();
RegisterConverters(new[]{newfooconverter()});
序列化程序=tmp;
}
返回序列化程序;
}
}
}
静态类程序{
静态void Main()
{
var obj=newfoo{imahappycamp=true,Name=“Fred”};
字符串s=Foo.Serializer.Serialize(obj);
}
}
我会使用匿名类型来保持生成的JSON干净
class SomeClass {
public string WantedProperty { get; set; }
public string UnwantedProperty { get; set; }
}
var objects = new List<SomeClass>();
...
new JavaScriptSerializer().Serialize(
objects
.Select(x => new {
x.WantedProperty
}).ToArray()
);
class-SomeClass{
公共字符串WantedProperty{get;set;}
公共字符串UnwantedProperty{get;set;}
}
var objects=新列表();
...
新的JavaScriptSerializer().Serialize(
物体
.选择(x=>new{
x、 想要的财产
}).ToArray()
);
我会使用匿名类型来保持生成的JSON干净
class SomeClass {
public string WantedProperty { get; set; }
public string UnwantedProperty { get; set; }
}
var objects = new List<SomeClass>();
...
new JavaScriptSerializer().Serialize(
objects
.Select(x => new {
x.WantedProperty
}).ToArray()
);
class-SomeClass{
公共字符串WantedProperty{get;set;}
公共字符串UnwantedProperty{get;set;}
}
var objects=新列表();
...
新的JavaScriptSerializer().Serialize(
物体
.选择(x=>new{
x、 想要的财产
}).ToArray()
);