How to convert JSON text into objects using C#

How can I convert the following JSON response to a C# object?

{ "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"]]] ] } 
4

6 Answers

To create a class off a json string, copy the string.

In Visual Studio, click Edit > Paste special > Paste Json as classes.

5

First create a class to represent your json data.

public class MyFlightDto { public string err_code { get; set; } public string org { get; set; } public string flight_date { get; set; } // Fill the missing properties for your data } 

Using Newtonsoft JSON serializer to Deserialize a json string to it's corresponding class object.

var jsonInput = "{ org:'myOrg',des:'hello'}"; MyFlightDto flight = Newtonsoft.Json.JsonConvert.DeserializeObject<MyFlightDto>(jsonInput); 

Or Use JavaScriptSerializer to convert it to a class(not recommended as the newtonsoft json serializer seems to perform better).

string jsonInput="have your valid json input here"; // JavaScriptSerializer jsonSerializer = new JavaScriptSerializer(); Customer objCustomer = jsonSerializer.Deserialize<Customer >(jsonInput) 

Assuming you want to convert it to a Customer classe's instance. Your class should looks similar to the JSON structure (Properties)

2

I recommend you to use JSON.NET. it is an open source library to serialize and deserialize your c# objects into json and Json objects into .net objects ...

Serialization Example:

Product product = new Product(); product.Name = "Apple"; product.Expiry = new DateTime(2008, 12, 28); product.Price = 3.99M; product.Sizes = new string[] { "Small", "Medium", "Large" }; string json = JsonConvert.SerializeObject(product); //{ // "Name": "Apple", // "Expiry": new Date(1230422400000), // "Price": 3.99, // "Sizes": [ // "Small", // "Medium", // "Large" // ] //} Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json); 

Performance Comparison To Other JSON serializiation Techniques enter image description here

4

copy your Json and paste at textbox on and click on Generate button,

A cs class will be generated use that cs file as below:

var generatedcsResponce = JsonConvert.DeserializeObject(yourJson);

where RootObject is the name of the generated cs file;

1

This will take a json string and turn it into any class you specify

public static T ConvertJsonToClass<T>(this string json) { System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); return serializer.Deserialize<T>(json); } 
class Program { static void Main(string[] args) { var res = Json; //Json that has to be converted Response resp = new Response(); resp = JsonSerializer.Deserialize<Response>(res); Console.WriteLine(res); } } public class Response { public bool isValidUser { get; set; } public string message { get; set; } public int resultKey { get; set; } } 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like