一圆恤、JSON使用JsonPropertyAttribute重命名屬性名
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;
namespace JSONDemo
{
public class Movie
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("Chinese Director")]
public string Director { get; set; }
public int ReleaseYear { get; set; }
}
}
二扫尖、JSON使用JsonPropertyAttribute序列化升序排序?qū)傩?/p>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;
namespace JSONDemo
{
public class Movie
{
[JsonProperty(Order=4)]
public string Name { get; set; }
[JsonProperty(Order=0)]
public string Director { get; set; }
public int ReleaseYear { get; set; }
[JsonProperty(Order=-3)]
public string ChiefActor { get; set; }
[JsonProperty(Order=2)]
public string ChiefActress { get; set; }
}
三膊夹、JSON使用JsonPropertyAttribute反序列化屬性時(shí),Required指定屬性性質(zhì)
.創(chuàng)建一個(gè)Movie對(duì)象婆瓜,給屬性添加JsonProperty,并指定其Required的性質(zhì)贡羔。屬性Name必須有值廉白,DateTime可以為空
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;
namespace JSONDemo
{
public class Movie
{
[JsonProperty(Required=Required.Always)]
public string Name { get; set; }
[JsonProperty(Required = Required.AllowNull)]
public DateTime? ReleaseDate { get; set; }
public string Director { get; set; }
}
}
四、JSON使用JsonPropertyAttribute序列化引用類型集合
1.創(chuàng)建一個(gè)Director對(duì)象乖寒,并聲明一個(gè)本身類型的屬性猴蹂,指定JsonProperty中的IsReference為true.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;
namespace JSONDemo
{
public class Director
{
public string Name { get; set; }
[JsonProperty(IsReference=true)]
public Director ExecuteDir { get; set; }
}
}
2.創(chuàng)建一個(gè)Movie對(duì)象,聲明一個(gè)Director集合的屬性楣嘁,指定JsonProperty中的IsReference為true.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;
namespace JSONDemo
{
public class Movie
{
public string Name { get; set; }
[JsonProperty(ItemIsReference=true)]
public IList<Director> Directors { get; set; }
}
}
3.序列化對(duì)象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json;
using GongHuiNewtonsoft.Json.Serialization;
using GongHuiNewtonsoft.Json.Converters;
namespace JSONDemo
{
class Program
{
static void Main(string[] args)
{
Director dir = new Director
{
Name = "馮小剛"
};
Director dir1 = new Director
{
Name = "張藝謀",
ExecuteDir = dir
};
Movie m = new Movie
{
Name = "滿城盡帶黃金甲",
Directors = new List<Director>
{
dir,
dir1
}
};
string json = JsonConvert.SerializeObject(m, Formatting.Indented);
Console.WriteLine(json);
}
}
}
五磅轻、JSON使用JsonPropertyAttribute序列化忽略屬性null
1.創(chuàng)建一個(gè)Movie對(duì)象珍逸,并在屬性上指定JsonProperty,添加NullValueHandling聋溜,忽略null
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GongHuiNewtonsoft.Json;
namespace JSONDemo
{
public class Movie
{
public string Name { get; set; }
public string Director { get; set; }
[JsonProperty(NullValueHandling=NullValueHandling.Ignore)]
public DateTime? LaunchDate { get; set; }
}
}
2.實(shí)例化對(duì)象Movie對(duì)象谆膳,然后序列化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using GongHuiNewtonsoft.Json;
using GongHuiNewtonsoft.Json.Serialization;
using GongHuiNewtonsoft.Json.Converters;
namespace JSONDemo
{
class Program
{
static void Main(string[] args)
{
Movie m = new Movie
{
Name = "愛情呼叫轉(zhuǎn)移",
Director = "張建亞"
};
string json = JsonConvert.SerializeObject(m, Formatting.Indented);
Console.WriteLine(json);
}
}
}