从OpenBadge的POCO类序列化中省略一个字段

时间:2014-03-05 17:04:08

标签: json serialization openbadge

我有以下POCO课程:

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标记会处理这个问题。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

使用Newtonsoft.Json JsonCovert.SerializeObject进行序列化时,可以使用[JsonIgnore]属性忽略属性。

将您的密钥属性修改为:

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