Use Newtonsoft or LINQ to JSON to deserialize JSON in .NET
Question:
How to convert JSON data into usable .NET objects using Newtonsoft or LINQ to JSON?
Answer:
Use Newtonsoft.Json:
Use LINQ to JSON:
Use C# dynamic typing:
Sample code using dynamic typing:
<code class="language-csharp">public class Example { public int Id { get; set; } public string Name { get; set; } } // JSON字符串 string json = "{\"Id\": 1, \"Name\": \"biofractal\"}"; // 反序列化为动态对象 dynamic results = JsonConvert.DeserializeObject<dynamic>(json); // 访问属性 int id = results.Id; string name = results.Name; // 如需创建强类型对象 Example example = new Example { Id = id, Name = name, };</code>
The above is the detailed content of How to Deserialize JSON in .NET using Newtonsoft or LINQ to JSON?. For more information, please follow other related articles on the PHP Chinese website!