2014-03-05 13 views
0

我有以下的POCO類:省略從POCO類的序列化的字段OpenBadge

public class BadgeClass 
{ 
    [Key] 
    [ScriptIgnore()] 
    //Internal ID not used for sending information to the OpenBadges API 
    public int BadgeID { get; set; } 

    //The name of the achievement. 
    [StringLength(128)] 
    public string Name { get; set; } 

    //A short description of the achievement. 
    [StringLength(128)] 
    public string Description { get; set; } 

    //URL of an image representing the achievement. Should be a square and in PNG format. Maximum size is 256kb. 
    public string Image { get; set; } 

    //URL of the criteria for earning the achievement. If the badge represents an educational achievement, consider marking 
    //up this up with LRMI 
    public string Criteria { get; set; } 

    //URL of the organization that issued the badge. Endpoint should be an IssuerOrganization 
    public string Issuer { get; set; } 

    //List of objects describing which educational standards this badge aligns to, if any. 
    //public List<AlignmentObject> Alignments { get; set; } 

    //List of tags that describe the type of achievement. 
    //public List<string> Tags { get; set; } 
} 

我試圖對象序列化到JSON和使用下面的代碼寫入到本地文件:

var json = JsonConvert.SerializeObject(newBadge); 
System.IO.File.WriteAllText(@"c:\test.json", json); 

這是工作太多,因爲JSON文件正在創建,但我想從序列化中省略BadgeClassID字段。我認爲ScriptIgnore標記會照顧到這一點。有沒有辦法做到這一點?

回答

0

使用Newtonsoft.Json JsonCovert.SerializeObject進行序列化時,可以使用[JsonIgnore]屬性忽略屬性。

修改您的主要特性如下:

[Key] 
[JsonIgnore] 
//Internal ID not used for sending information to the OpenBadges API 
public int BadgeID { get; set; }