Convert JSON String To C# Object

前端 未结 14 1959
感情败类
感情败类 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:40

    You can accomplished your requirement easily by using Newtonsoft.Json library. I am writing down the one example below have a look into it.

    Class for the type of object you receive:

    public class User
    {
        public int ID { get; set; }
        public string Name { get; set; }
    
    }
    

    Code:

    static void Main(string[] args)
    {
    
          string json = "{\"ID\": 1, \"Name\": \"Abdullah\"}";
    
          User user = JsonConvert.DeserializeObject(json);
    
          Console.ReadKey();
    }
    

    this is a very simple way to parse your json.

提交回复
热议问题