如何将c#对象的某些字段序列化为JSON
假设我们有这个物体如何将c#对象的某些字段序列化为JSON,c#,javascript,json,C#,Javascript,Json,假设我们有这个物体 public class Person{ public string firstName { get; set; } public string lastName { get; set; } public DateTime dob { get; set; } } 我将返回这些人员的列表,我需要将这些人员对象序列化为json格式 我可以做全部 using System.Web.Script.Serialization; ... var peopleLi
public class Person{
public string firstName { get; set; }
public string lastName { get; set; }
public DateTime dob { get; set; }
}
我将返回这些人员的列表,我需要将这些人员对象序列化为json格式
我可以做全部
using System.Web.Script.Serialization;
...
var peopleList = new List<Person> { person_1, person_2 }
var jsonPeople = new JavaScriptSerializer().Serialize(peopleList);
return jsonPeople
使用System.Web.Script.Serialization;
...
var peopleList=新列表{person_1,person_2}
var jsonPeople=newJavaScriptSerializer().Serialize(peopleList);
返回jsonPeople
但是,我是否可以只获取名字和姓氏,而不是序列化整个对象及其所有字段/属性
如果无法编辑
人员
,则需要添加自定义转换器:
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
static class Program
{
static void Main()
{
var ser = new JavaScriptSerializer();
ser.RegisterConverters(new[] { new PersonConverter() });
var person = new Person
{
firstName = "Fred",
lastName = "Jones",
dob = new DateTime(1970, 1, 1)
};
var json = ser.Serialize(person);
Console.WriteLine(json);
}
}
class PersonConverter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get { yield return typeof(Person); }
}
public override IDictionary<string, object> Serialize(
object obj, JavaScriptSerializer serializer)
{
var person = (Person)obj;
return new Dictionary<string, object> {
{"firstName", person.firstName },
{"lastName", person.lastName }
};
}
public override object Deserialize(
IDictionary<string, object> dictionary, Type type,
JavaScriptSerializer serializer)
{
object tmp;
var person = new Person();
if (dictionary.TryGetValue("firstName", out tmp))
person.firstName = (string)tmp;
if (dictionary.TryGetValue("lastName", out tmp))
person.lastName = (string)tmp;
return person;
}
}
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public DateTime dob { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
静态类程序
{
静态void Main()
{
var ser=新的JavaScriptSerializer();
ser.RegisterConverter(new[]{new PersonConverter()});
var person=新的人
{
firstName=“Fred”,
lastName=“琼斯”,
dob=新日期时间(1970年,1月1日)
};
var json=ser.Serialize(person);
Console.WriteLine(json);
}
}
类PersonConverter:JavaScriptConverter
{
公共覆盖IEnumerable SupportedTypes
{
获取{yield return typeof(Person);}
}
公共重写IDictionary序列化(
对象obj,JavaScriptSerializer(序列化程序)
{
var person=(person)obj;
返回新词典{
{“firstName”,person.firstName},
{“lastName”,person.lastName}
};
}
公共重写对象反序列化(
I字典,类型,
JavaScriptSerializer(序列化程序)
{
对象tmp;
var person=新的person();
if(dictionary.TryGetValue(“firstName”,out-tmp))
person.firstName=(字符串)tmp;
if(dictionary.TryGetValue(“lastName”,out tmp))
person.lastName=(字符串)tmp;
返回人;
}
}
公共阶层人士
{
公共字符串名{get;set;}
公共字符串lastName{get;set;}
公共日期时间dob{get;set;}
}
如果无法编辑
人员
,则需要添加自定义转换器:
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
static class Program
{
static void Main()
{
var ser = new JavaScriptSerializer();
ser.RegisterConverters(new[] { new PersonConverter() });
var person = new Person
{
firstName = "Fred",
lastName = "Jones",
dob = new DateTime(1970, 1, 1)
};
var json = ser.Serialize(person);
Console.WriteLine(json);
}
}
class PersonConverter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
get { yield return typeof(Person); }
}
public override IDictionary<string, object> Serialize(
object obj, JavaScriptSerializer serializer)
{
var person = (Person)obj;
return new Dictionary<string, object> {
{"firstName", person.firstName },
{"lastName", person.lastName }
};
}
public override object Deserialize(
IDictionary<string, object> dictionary, Type type,
JavaScriptSerializer serializer)
{
object tmp;
var person = new Person();
if (dictionary.TryGetValue("firstName", out tmp))
person.firstName = (string)tmp;
if (dictionary.TryGetValue("lastName", out tmp))
person.lastName = (string)tmp;
return person;
}
}
public class Person
{
public string firstName { get; set; }
public string lastName { get; set; }
public DateTime dob { get; set; }
}
使用系统;
使用System.Collections.Generic;
使用System.Web.Script.Serialization;
静态类程序
{
静态void Main()
{
var ser=新的JavaScriptSerializer();
ser.RegisterConverter(new[]{new PersonConverter()});
var person=新的人
{
firstName=“Fred”,
lastName=“琼斯”,
dob=新日期时间(1970年,1月1日)
};
var json=ser.Serialize(person);
Console.WriteLine(json);
}
}
类PersonConverter:JavaScriptConverter
{
公共覆盖IEnumerable SupportedTypes
{
获取{yield return typeof(Person);}
}
公共重写IDictionary序列化(
对象obj,JavaScriptSerializer(序列化程序)
{
var person=(person)obj;
返回新词典{
{“firstName”,person.firstName},
{“lastName”,person.lastName}
};
}
公共重写对象反序列化(
I字典,类型,
JavaScriptSerializer(序列化程序)
{
对象tmp;
var person=新的person();
if(dictionary.TryGetValue(“firstName”,out-tmp))
person.firstName=(字符串)tmp;
if(dictionary.TryGetValue(“lastName”,out tmp))
person.lastName=(字符串)tmp;
返回人;
}
}
公共阶层人士
{
公共字符串名{get;set;}
公共字符串lastName{get;set;}
公共日期时间dob{get;set;}
}
是否仍然可以在不接触人员类别代码的情况下执行此操作?@loonyuni-yes;给我几分钟-那更复杂哇,那更复杂,谢谢你的详细例子!我想我有很多转换器要写!是否仍然可以在不接触person类代码的情况下执行此操作?@loonyuni-yes;给我几分钟-那更复杂哇,那更复杂,谢谢你的详细例子!我想我有很多转换器要写!