0

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

4

1 回答 1

0

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

修改您的关键属性为:

[Key]
[JsonIgnore]
//Internal ID not used for sending information to the OpenBadges API
public int BadgeID { get; set; }
于 2014-10-21T01:00:34.127 回答