How to Deserialize JSON data? C#

前端 未结 5 1571
感情败类
感情败类 2020-12-22 10:22

Im getting a Json Data from an API and i have been trying to deserialize.

Json data:

{
   \"items\": [
      {
         \"id\": \"1\",
         \"nam         


        
5条回答
  •  南方客
    南方客 (楼主)
    2020-12-22 10:54

    Your entities(models) look just fine. If you are using, or were to use ASP.NET Web API 2, and your client is using the http verb post for example, this setup would work as Web API takes care of the object deserialization:

        public HttpStatusCode Post(Item item)
        {
           Debug.Write(item.toString());
           return HttpStatusCode.OK;
        }
    

    If you insist in deserializing manually then use the JavaScriptSerializer library which allows you to do things like:

    Item item = new JavaScriptSerializer().Deserialize(content);
    

    Notice that .Deserialize() takes a generic which in your case it Item.

    Hope that helps.

提交回复
热议问题