Home >Backend Development >C++ >How Can I Deserialize JSON into C# Dynamic Objects?

How Can I Deserialize JSON into C# Dynamic Objects?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2025-02-03 01:41:09501browse

How Can I Deserialize JSON into C# Dynamic Objects?

Serially sequence of JSON to C#dynamic object

This article discusses the dissection of JSON data into a C#dynamic type, so as to avoid creating a large number of classes to use DataContractJSONSERIALIZER.

Solution using json.net

JSON.NET provides a way to easily achieve dynamic JSON degradation:

Solution using newtonsoft.json.linq

<code class="language-csharp">dynamic stuff = JsonConvert.DeserializeObject("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

string name = stuff.Name;
string address = stuff.Address.City;</code>

Similarly, newtonsoft.json.linq also provides another option:

More resources

<code class="language-csharp">dynamic stuff = JObject.Parse("{ 'Name': 'Jon Smith', 'Address': { 'City': 'New York', 'State': 'NY' }, 'Age': 42 }");

string name = stuff.Name;
string address = stuff.Address.City;</code>
If you need to learn more, please refer to the part of the official document on "Use Dynamic Query JSON" part: [Link to Document]

The above is the detailed content of How Can I Deserialize JSON into C# Dynamic Objects?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn