How to map JSON to C# Objects

后端 未结 4 693
忘掉有多难
忘掉有多难 2020-12-01 03:13

I am having issues with understanding how to make this happen.

Basically we have an API, the user sends a JSON of the format: (excuse the code if not perfect but yo

4条回答
  •  再見小時候
    2020-12-01 03:32

    Easiest way I know is to use JSON.Net by newtonsoft. To make it easier to understand, I always make matching classes in C# with the same name. Then its easier to deserialize it. As an example, if it is an array of objects in js, it will map to a list of object with the same names in C#. As for the date time, its a bit tricky. Id do the client side validation and Datetime.tryParse in the serverside, itll take care of the dashes or slashes.

    var serializer = new JavaScriptSerializer();
    List abcList = serializer.Deserialize>(PassedInJsonString);
    

提交回复
热议问题