Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JSON Object Empty when is Serialize it C#?

I am using the following Code

public class GetTabelRealizari : ControllerBase
{

    public class Realizare
    {
        String user;
        String denumire;
        String incasari;

        public Realizare(String user, String denumire, String incasari)
        {
            this.user = user;
            this.denumire = denumire;
            this.incasari = incasari;
        }

        public String getUser()
        {
            return user;
        }

        public void setUser(String user)
        {
            this.user = user;
        }

        public String getDenumire()
        {
            return denumire;
        }

        public void setDenumire(String denumire)
        {
            this.denumire = denumire;
        }

        public String getIncasari()
        {
            return incasari;
        }

        public void setIncasari(String incasari)
        {
            this.incasari = incasari;
        }
    }


    [HttpPost]
    public string Post([FromBody] string[] value)
    {
        //SSMS connection
        string connectionString = "Data Source=DESKTOP-QKC0G7V;Initial Catalog=Restaurant_gest;Integrated Security=True";
        SqlConnection connection = new SqlConnection(connectionString);
        connection.Open();

        List<Realizare> realizari = new List<Realizare>();

        double incasari;
        String incasariString;

        SqlCommand command = new SqlCommand("SELECT Users.Username," +
            " Tip_Nota_Plata.Denumire," +
            " sum(Nota_plata.Suma) as Incasari" +
            " from Users" +
            " INNER JOIN Nota_plata" +
            " INNER JOIN Comandas" +
            " ON Nota_plata.Id_comanda = Comandas.Id" +
            " ON Comandas.User_Id = Users.Id" +
            " INNER JOIN Tip_Nota_Plata" +
            " ON Tip_Nota_Plata.Id = Nota_plata.Id_tip_nota" +
            " Group by Username, Tip_Nota_Plata.Denumire", connection);

        using (SqlDataReader reader = command.ExecuteReader())
        {
            while (reader.Read())
            {
                incasari = (double)reader["Incasari"];
                incasariString = incasari.ToString("#.##");

                realizari.Add(new Realizare(reader["Username"].ToString(), reader["Denumire"].ToString(), incasariString));

            }
        }

        return JsonConvert.SerializeObject(realizari);
        //return "salut";
    }
}

And I am receiving an empty JsonObject. Why?
[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]

I keep trying to make it work and I cannot. The list has the objects, i can test it with Console.Writeline(realizari[0].getDenumire()) and it works. I can also serialize a list of strings, it just doesn`t work for objects.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Because the object has no serializable properties.

I’m going to guess you are a Java developer based on this:

String user;

public String getUser()
{
    return user;
}

public void setUser(String user)
{
    this.user = user;
}

C# has "properties" which, while they compile down to methods very similar to this, the syntax in C# is a bit different. All of the above code can be simplified to a property:

public String User { get; set; }

The usage then becomes simpler as well, allowing for assignments instead of calling a method:

someObject.User = someUser;

In cases where you want to add logic to your getter/setter, you can expand the "auto implemented property" above into a manual one:

private string user;

public string User
{
    get { return user; }
    set { user = value; }
}

The get and set syntax still tells the compiler that this is a property, but within those blocks you can write any method logic you like. (In the setter value is a keyword for the value being assigned to the property.)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading