繁体   English   中英

如何通过json.net序列化单个值?

[英]How Serialize single value by json.net?

我正在使用json.net序列化Student类。

我想向序列化对象添加一个属性(称为cheak ,它不是Student类的成员。

这是班上的学生:

public class Student 
    {
        public int ID { get; set; }
        [JsonIgnore]
        public Faculty Faculty { get; set; }
        [JsonProperty("Faculty")]
        public string FacultyName { get { return Faculty.name; } }
        public double AVG { get; set; }
        public DateTime DateOfBirth { get; set; }
        public string EducationInfo { get; set; }
        public string FatherName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MotherName { get; set; }
        public string Password { get; set; }
        public string PersonalInfo { get; set; }
    }

json:

{
  "ID": 24,
  "Faculty": "engen ",
  "AVG": 3.0,
  "DateOfBirth": "1990-02-02T00:00:00",
  "EducationInfo": "GOOD",
  "FatherName": "EEWF",
  "FirstName": "FFEWR",
  "LastName": "ERF",
  "MotherName": "ERF",
  "Password": null,
  "PersonalInfo": "ERF",
}

我想将属性“ cheak”添加到序列化对象中:

{
  **cheak:true
  "ID": 24,
  "Faculty": "engen ",
  "AVG": 3.0,
  "DateOfBirth": "1990-02-02T00:00:00",
  "EducationInfo": "GOOD",
  "FatherName": "EEWF",
  "FirstName": "FFEWR",
  "LastName": "ERF",
  "MotherName": "ERF",
  "Password": null,
  "PersonalInfo": "ERF",
}

如果您需要能够反序列化和序列化对象,则可以将Student继承到这样的新类上

public class StudentJson : Student 
{
    public bool cheak { get; set; }
}

然后,您可以将其传递给序列化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM