Convert JSON String To C# Object

前端 未结 14 1960
感情败类
感情败类 2020-11-22 14:27

Trying to convert a JSON string into an object in C#. Using a really simple test case:

JavaScriptSerializer json_serializer = new JavaScriptSerializer();
obj         


        
14条回答
  •  难免孤独
    2020-11-22 14:52

    First you have to include library like:

    using System.Runtime.Serialization.Json;
    
    DataContractJsonSerializer desc = new DataContractJsonSerializer(typeof(BlogSite));
    string json = "{\"Description\":\"Share knowledge\",\"Name\":\"zahid\"}";
    
    using (var ms = new MemoryStream(ASCIIEncoding.ASCII.GetBytes(json)))
    {
        BlogSite b = (BlogSite)desc.ReadObject(ms);
        Console.WriteLine(b.Name);
        Console.WriteLine(b.Description);
    }
    

提交回复
热议问题