在需要转JSON的对象类头中加入
[DataContract(IsReference = true)] 属性
在类中的每个属性都添加
[JsonProperty]
属性
1 [DataContract(IsReference = true)] 2 public class Project 3 { 4 [JsonProperty] 5 public virtual int id 6 { 7 get; 8 set; 9 } 10 [JsonProperty] 11 public virtual string name 12 { 13 get; 14 set; 15 } 16 [JsonProperty] 17 public virtual int payScore 18 { 19 get; 20 set; 21 } 22 [JsonProperty] 23 public virtual int score 24 { 25 get; 26 set; 27 } 28 [JsonProperty] 29 public virtual int userID 30 { 31 get; 32 set; 33 } 34 35 [JsonProperty] 36 public virtual ISet<Aduit> Aduits 37 { 38 get; 39 set; 40 } 41 // public virtual List<SingleForm> singleFormList 42 }
再取出JSON时
1 var result = projectManager.MyProject(id); 2 JsonSerializerSettings settings = new JsonSerializerSettings(); 3 settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 4 string s = JsonConvert.SerializeObject(result, settings); 5 return s;