Home > OS >  JSON Object Empty when is Serialize it C#?
JSON Object Empty when is Serialize it C#?

Time:07-21

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.

CodePudding user response:

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.)

  • Related