0

I have the following JSON that is received in my webAPI endpoint.

{"time":1507739203,"messages":{"closedChat":[{"id":1280091,"channel":"widget","closed_at":"2017-10-11 13:26:43","tabulation":false}]},"token":"5df3c296972ffa6972"}

I am able to get the "time" variable in my controller but I'm not able to get the "closedChat" collection, always returns null.

here is my controller:

[AllowAnonymous]
[Route("api/test1/closedChat")]
[HttpPost]
public string closedChat([FromBody] rag msg)
{
    string messageTime = msg.time; //this works
    List<Fimmsg> teste2 = msg.messages; //this do not

    return messageTime;
}

here is my rag class created to receive the JSON return:

public class rag
    {
        [JsonProperty("time")]
        public string time { get; set; } //this works

        [JsonProperty("messages")]
        public List<Fimmsg> messages { get; set; } //this do not

    }

and I created a Fimmsg class to hold the "messages" array/List:

public class Fimmsg
{
        public int id { get; set; }
        public string channel { get; set; }
        public string closed { get; set; }
        public string tabulation { get; set; }
}

What Am I missing to get the 'closedchat' collection?

user77
  • 303
  • 1
  • 5
  • 12
  • 1
    `messages` is not a list, it's an object. `messages.closedChat` is a list. – CodeCaster Oct 16 '17 at 13:46
  • Go to http://json2csharp.com/ and enter your JSON in and it will create the necessary classes for you. Basically your issue is you have Messages as a collection but its actually an object that has a property which is the collection you are wanting. – Bearcat9425 Oct 16 '17 at 13:49
  • thanks @CodeCaster, you are right it's an object, the answers I found were about deserializing an simple json, but not receiving it from a controller and sendong to a class that has this objct inside, but it shed some light on the problem. – user77 Oct 16 '17 at 14:08
  • thanks @Bearcat9425, your answer shed even more light showing how to create the classes correctly to receive the values :) thanks all! – user77 Oct 16 '17 at 14:10
  • updated the classes according to jspn2charp.com and were able to get the values :) I assume that the 'post your answer' is not available because it was marked as duplicate.. is that true? – user77 Oct 16 '17 at 14:40
  • Yes thats why, there are a large number of questions very similar to yours out there. – Bearcat9425 Oct 16 '17 at 15:03

0 Answers0