Convert JSON string to net object

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video we will discuss
1. How to convert .NET object to JSON string
2. How to convert a JSON string to .NET object

We will be using the following Employee class
public class Employee
{
public string firstName { get; set; }
public string lastName { get; set; }
public string gender { get; set; }
public int salary { get; set; }
}

Replace < with LESSTHAN symbol and > with GREATERTHAN symbol

The following example converts List<Employee> objects to a JSON string. Serialize() method of JavaScriptSerializer class converts a .NET object to a JSON string. JavaScriptSerializer class is present in System.Web.Script.Serialization namespace.

using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace Demo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Employee employee1 = new Employee
{
firstName = "Todd",
lastName = "Grover",
gender = "Male",
salary = 50000
};

Employee employee2 = new Employee
{
firstName = "Sara",
lastName = "Baker",
gender = "Female",
salary = 40000
};

List<Employee> listEmployee = new List<Employee>();
listEmployee.Add(employee1);
listEmployee.Add(employee2);

JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
string JSONString = javaScriptSerializer.Serialize(listEmployee);

Response.Write(JSONString);
}
}
}

Output :
[{"firstName":"Todd","lastName":"Grover","gender":"Male","salary":50000},{"firstName":"Sara","lastName":"Baker","gender":"Female","salary":40000}]

The following example converts a JSON string to List<Employee> objects. Deserialize() method of JavaScriptSerializer class converts a JSON string to List<Employee> objects.

string jsonString = "[{\"firstName\":\"Todd\",\"lastName\":\"Grover\",\"gender\":\"Male\",\"salary\":50000},{\"firstName\":\"Sara\",\"lastName\":\"Baker\",\"gender\":\"Female\",\"salary\":40000}]";

JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
List<Employee> employees = (List<Employee>)javaScriptSerializer.Deserialize(jsonString, typeof(List<Employee>));

foreach(Employee employee in employees)
{
}
Рекомендации по теме
Комментарии
Автор

Nice explanation.. simple and easy to understand.. tqvm

beeqeem
Автор

Year 2024 - Still the best demonstration on YouTube.

Some are born star professor

faizanhaque
Автор

Thanks Venkat !!
would you please record one video how to use this JSON in MVC.
Passing Employee object from view to Controller and vice versa

ruthyayele
Автор

Json is an awesome idea to keep complex data structures in database. Good explanation. Thanks a lot.

alibekalmeshev
Автор

Thank you Venkat, awesome video, it helped me download data from Github.

shekelboi
Автор

That was a very clear and useful explanation. Thanks for the clarity

JVPTurnerFamily
Автор

Very good job, everyone can learn with this video, thank you sir!

mariomora
Автор

hi from Mexico
this code is works
List<Employee> listEmployee =

amigowisp
Автор

if you make tutorials on php then many new php learners like me will be benefited..

trimedu
Автор

Hi, could you do a vid on getting nested elemnets in JSON using recursion please

GiorgiSukhitashvili
Автор

I found that Newtonsoft.Json did a better job at parsing JSON strings with embedded line feeds and carriage returns. For example:

Dim json As reCaptchaJSONResult
json = reCaptchaJSONResult)(s) ' s has vbLf or vbCr or vbCrLf

Note that reCaptchaJSONResult is a model class mirroring the structure of s.

lynndemarest
Автор

Great explanation, than you very much!!

braScene
Автор

Sir please make a video on how to read and write an excel through c#

sindug
Автор

Hi. Venkat I am getting an error while using this function . the error is --Uncaught TypeError: JSON.Stringify is not a function(…).
Please, Help me to solve this.

ashokkoiri
Автор

Please cover converting json objects that use datetimes. That will catch someone up as soon as they try. The json produced for dates looks un-legible. It comes out like like this:
"{
"name": "venkat",
"salary": 50, 000,
"lastActivity": /Date(42899090909090293029)
}"

mattmarkus
Автор

Thanks a lot. This has helped me a lot

bhavanapande
Автор

What do you do if the Json parameter name is different than your class property name?

TheOrneryNerd
Автор

Hello nice tuto !
Do you know how to put a link href inside a json file ?

christianmilfort
Автор

Hello, my json file is quite large. how do I ingest the file without pasting the json string to deserialize the file?

mariummomo
Автор

can we convert php object into json string in same way?

RishavKumar-xgto