1. 序列化相关技巧
忽略某些属性
有时候我们会有这样的需求,我们只需要序列化实体类中的一部分属性,这时候我们可以通过声明忽略掉一些我们不需要序列化的属性,有两种方式可以使用么达到这个目标:
首先,可以考虑使用JsonIgnore
特性修饰不需要进行序列化的属性,如下所示:
public class Employee
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public decimal Salary { get; set; }
public string Phone { get; set; }
[JsonIgnore]
public DateTime HireDate { get; set; }
}
运行程序:
var employeeBean = new EmployeeBean()
{
Id = Guid.NewGuid(),
Name = "gyzhao",
Email = "gyzhao@gyzhao.com",
Salary = 10000,
Phone = "13912390987",
HireDate = new DateTime(2012, 2, 1)
};
var jsonString = JsonConvert.SerializeObject(employeeBean, Formatting.Indented);
//输出:
//{
// "Id": "69a406ad-902c-45d3-8ba7-89a09779ed52",
// "Name": "gyzhao",
// "Email": "gyzhao@gyzhao.com",
// "Salary": 10000.0,
// "Phone": "13912390987"
//}
如果说你需要序列化的类有很多的属性,而你是需要使用其中的一小部分,如果使用上面的上面方式就会比较繁琐(因为需要忽略的属性太多了),这时候可以考虑使用DataContract
特性修饰被序列化的类,使用DataMember
特性修饰需要进行序列化的属性,其他没有该特性属性会被自动忽略掉。如下所示:
[DataContract]
public class EmployeeBean
{
[DataMember]
public Guid Id { get; set; }
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
[DataMember]
public decimal Salary { get; set; }
public string Phone { get; set; }
public DateTime? HireDate { get; set; }
}
运行程序:
var employeeBean = new EmployeeBean()
{
Id = Guid.NewGuid(),
Name = "gyzhao",
Email = "gyzhao@gyzhao.com",
Salary = 10000,
Phone = "13912390987",
HireDate = new DateTime(2012, 2, 1)
};
var jsonString = JsonConvert.SerializeObject(employeeBean, Formatting.Indented);
//输出:
//{
// "Id": "69a406ad-902c-45d3-8ba7-89a09779ed52",
// "Name": "gyzhao",
// "Email": "gyzhao@gyzhao.com",
// "Salary": 10000.0
//}
DataContract
特性和DataMember
特性都从属于:System.Runtime.Serialization
命名空间。
序列化对象时循环引用异常的解决办法
序列化一个对象时,如果该对象有一个集合属性,改集合的类型就是对象本身的话,默认序列化的方法会报一个循环引用的异常,如果需要序列化,只需声明下面的属性即可:
JsonConvert.SerializeObject(result,new JsonSerializerSettings{ReferenceLoopHandling=ReferenceLoopHandling.Serialize})
2. 反序列化相关技巧
2.1 使用匿名类型作为反序列化实体
var jsonString = @"{
'Id': '69a406ad-902c-45d3-8ba7-89a09779ed52',
'Name': 'gyzhao',
'Salary': 10000.0,
'HireDate': '2012-02-01T00:00:00'
}";
var employee = new
{
Name = default(string),
Salary = default(decimal),
HireDate = default(DateTime),
Id = default(Guid)
};
var employeeBean = JsonConvert.DeserializeAnonymousType(jsonString, employee);
3. 创建JSON
//命令式的创建JSON对象
var array = new JArray();
var text = new JValue("Manual text");
var date = new JValue(DateTime.Now);
array.Add(text);
array.Add(date);
Console.WriteLine(array.ToString());
//使用声明式的语法
var rss =
new JObject(
new JProperty("channel", new JObject(
new JProperty("title", "James Nexton-king"),
new JProperty("link", "http://james.newtonking.com"),
new JProperty("description", "James Newton-Kin's blog."),
new JProperty("item", "BB"))));
Console.WriteLine(rss.ToString());
//通过一个匿名对象创建JSON
JObject o = JObject.FromObject(new
{
channel = new
{
title = "James Newton-king",
link = "http://james.netwoing.com",
item = new List<string>()
{
"A",
"B",
"C",
"D",
"E"
}
}
});
Console.WriteLine(o.ToString());
参考&进一步阅读
http://www.newtonsoft.com/json
时间: 2024-11-06 12:39:14