简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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