Empty JSON objects ([{}, {}, ..]) are written to the file

im trying to write a simple list for json.

no errors, it works fine, but I get this output

[{},{},{}]

here is a snippet of my code. studentList is a list of objects of class Student.

    public void jsonRead()
    {
        string json = File.ReadAllText(Environment.CurrentDirectory + @"\JSON.txt");

        studentList= new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<List<Student>>(json);
    }

    public void jsonWrite()
    {

            string json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(studentList);

            File.WriteAllText(Environment.CurrentDirectory + @"\JSON.txt", json);

    }

student class

class Student : IComparable
{
    private String regID {get;set;}   
    private String name {get;set;}      
    private String address {get;set;}      
    private String gender {get;set;}      
    private Double gpa {get;set;}

    public Student()
    {
        regID = null;
        name = null;
        address = null;
        gender = null;
        gpa = 0.0;
    }
    public Student(String regID, String name, String address, String gender, Double gpa)
    {
        this.regID = regID;
        this.name = name;
        this.address = address;
        this.gender = gender;
        this.gpa = gpa;
    }

    public void update(String regID, String name, String address, String gender, Double gpa)
    {
        setRegId( regID);
        setName(name);
        setAddress(address);
        setGender(gender);
        setGpa(gpa);
    }

followed by setters and getters

+4
source share
2 answers

found a solution. I don’t know if it fits right or not, but I just made all the data members public. he worked

+2
source

! ! ! Google , "[{}]". , . ( #):

public String Notes {private set; get}

"" . XML, JSON.

+1

Source: https://habr.com/ru/post/1526598/


All Articles