简体   繁体   中英

Asp.Net returns empty JSON instead of Message

I have this code, to get the messages from database:

[HttpGet]
        public string RecieveMessagesFromDatabase(int ChatID)
        {
// Before - Query formatting and execution
                while(reader.Read())
                {
                    for (int i = 0; i < UserList.Count; i++)
                    {
                        if (ChatID == 1)
                        {
                            TempMessageStorage.Add(
                                new Messages(reader["Message"].ToString(), Convert.ToInt32(reader["SenderID"]),
                                reader["Date"].ToString(), ReceiverID, groupid));
                        } // Public Chat
                        else if (ChatID == 2)
                        {
                            if (i == Convert.ToInt32(reader["SenderID"]) || i == Convert.ToInt32(reader["ReveiverID"]))
                            {
                                TempMessageStorage.Add(
                                new Messages(reader["Message"].ToString(), Convert.ToInt32(reader["SenderID"]),
                                reader["Date"].ToString(), ReceiverID, groupid));
                            }
                        } // Private Chat
                        else if(ChatID == 3)
                        {
                            if (i == Convert.ToInt32(reader["SenderID"]) || i == Convert.ToInt32(reader["ReveiverID"]))
                            {
                                TempMessageStorage.Add(
                                new Messages(reader["Message"].ToString(), Convert.ToInt32(reader["SenderID"]),
                                reader["Date"].ToString(), ReceiverID, groupid));
                            }
                        } // Group Chat
                    }
                }
                return JsonSerializer.Serialize(TempMessageStorage);
            }
            catch(Exception ex)
            {
                Content(ex.Message);
            }
            finally
            {
                reader.Close();
                _Connection.Connection.Close();
            }
            return null;
        }

In TempMessageStorage Messages are present, and it is not empty. But JSON returns the empty result. But not really empty. It seems to get the number of records, but they are empty. That's how it looks like - https://prnt.sc/u3d8gc

Messages class contains

在此处输入图像描述

I'm certain that in C# ASP.NET APIs you don't need to serializer/deserialize object because they will be returned as a json by default, image

[HttpGet]
public object Get()
{
    return new { number = 1, word = "hi" };
}

In the client side they would get a json describing that object, you don't need to serialize. The json would be like this:

{"number":1,"word":"hi"}

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