Home > Backend Development > C++ > How to Deserialize JSON Text into C# Objects?

How to Deserialize JSON Text into C# Objects?

Barbara Streisand
Release: 2025-01-01 13:32:11
Original
226 people have browsed it

How to Deserialize JSON Text into C# Objects?

Deserialize JSON Text into C# Objects

Deserializing JSON text into C# objects can be accomplished through a few simple steps.

For the provided JSON response:

{
  "err_code": "0",
  "org": "CGK",
  "des": "SIN",
  "flight_date": "20120719",
  "schedule": [
    ["W2-888", "20120719", "20120719", "1200", "1600", "03h00m", "737-200", "0", [["K", "9"], ["F", "9"], ["L", "9"], ["M", "9"], ["N", "9"], ["P", "9"], ["C", "9"], ["O", "9"]]],
    ["W2-999", "20120719", "20120719", "1800", "2000", "01h00m", "MD-83", "0", [["K", "9"], ["L", "9"], ["M", "9"], ["N", "9"]]]
  ]
}
Copy after login
  1. Visual Studio IDE: Click Edit > Paste Special > Paste JSON as Classes, which will generate C# classes for the JSON structure.
  2. NuGet: Install Newtonsoft.Json package for JSON serialization/deserialization functionality.
  3. Code: In your project, deserialize the JSON string into an object using the below code:
Rootobject r = Newtonsoft.Json.JsonConvert.DeserializeObject<Rootobject>(jsonString);
Copy after login

Replace "jsonString" with the variable containing the JSON text. Rename "Rootobject" with a more descriptive class name.

  1. Object Instance: The "r" variable now contains an instance of the C# object created from the deserialized JSON. You can access its properties to retrieve data, such as:
string errCode = r.err_code;
string org = r.org;
Copy after login

Follow these steps to effortlessly convert JSON text into C# objects, enabling you to work with structured data in your applications.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template