0

So I'm creating an endpoint using a data model called Chat that will accept data in this JSON form and store it in the database.

[{
    "ID": "123456",
    "Chat": [{
            "ID": "1",
            "Message": "User: that's a nice car Dylan: thanks",
            "PostedBy": "Dylan",
            "PostedOn": "2018-01-23T18:25:43.511Z"
        },
        {
            "ID": "2",
            "Message": "User: that's a really nice car Terry: thanks ",
            "PostedBy": "Terry",
            "PostedOn": "2018-02-23T18:25:43.511Z"
        },
        {
            "ID": "3",
            "Message": "User: that's the best car Roger: thanks",
            "PostedBy": "Roger",
            "PostedOn": "2018-03-23T18:25:43.511Z"
        }
    ]
}]

This is what I have currently and when I send data to the endpoint it only stores the ID, and nothing else in the database. Any thoughts/guidance is appreciated on how I could alter my model to accept the entirety of the data that is being sent.

public class Chat
{
    public string ID { get; set; }

    public string message { get; set; }

    public string postedBy { get; set; }

    public DateTime? postedOn { get; set;}
}
Paul Williams
  • 1,554
  • 7
  • 40
  • 75
  • 1
    You could start by using json2csharp.com – Rafael Jan 31 '18 at 22:06
  • You didnt show how you get from point A to point B, but "message" != "Message". Notice how the casing matches for the property that works? VS will build the class for you if you let it. Please read [ask] and take the [tour] – Ňɏssa Pøngjǣrdenlarp Jan 31 '18 at 22:06
  • Try matching the case of the properties... i.e. `Message`, `PostedBy` and `PostedOn` – Scott Perham Jan 31 '18 at 22:07
  • 1) Please specify the framework and/or serializer you are using. Is it [tag:wcf]? [tag:asp.net-web-api2]? [tag:asp.net-core]? Different frameworks use different serializers, and some are case-sensitive (`DataContractJsonSerializer`) while some are not (Json.NET). 2) Please share the actual signature of the endpoint method so we can see how it uses your current data model. A [mcve] would be ideal; without it we're just guessing. – dbc Jan 31 '18 at 23:33

2 Answers2

0

I may be wrong here, but it seems to me like you're using a class that represents a single message-instance (the class Chat) to attempt to store a whole list of Chat-data.

If I'm right, the only reason it actually stores ID is that it by chance happens to have the same name for two different levels in your data; one for the outer list (the whole set - and this is what is stored), and one for each of the inner chat-items.

Try to add this class, and use that instead (or rather in addition, since it actually contains a list of instances of your already existing class Chat):

public class ChatThread
{
    public string ID { get; set; }
    public IEnumerable<Chat> Chat { get; set; }
}
Kjartan
  • 18,591
  • 15
  • 71
  • 96
0

The ID which is being stored on your object is not the ID from the Chat object, but rather the higher ID definition which is common to your Chat objects.

You're were really close, if we discount the fact that you have not taken into consideration that C# is a case-sensitive language. The "higher" layer is composed of a String ID, but also of an array of Chat objects, so you should create a Class that holds the definition of these two properties.

public class JsonClass
{
    public string ID { get; set; }
    public Chat[] Chat { get; set; }
}

public class Chat
{
    public string ID { get; set; }
    public string Message { get; set; }
    public string PostedBy { get; set; }
    public DateTime PostedOn { get; set; }
}

Since there exist multiple Chat objects for the JsonClass ID property, you have to make it into a collection of some sort. I chose an array, but you can use other Collection objects, such as a List.

André B
  • 1,699
  • 2
  • 11
  • 22