ASP.NET JSON interview question: - How to send JSON format from ASP.NET?

191 1 0
                                    

JSON is a lightweight way of data exchange between two entities. If you want your ASP.NET page to emit JSON data, you need to use namespace “System.Runtime.Serialization” namsepace. To convert your c# classes to light weight JSON format we need to use “DataContractJsonSerializer” class as shown in the below code snippet.

Person myPerson = new Person("Chris", "Pietschmann");

System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = newSystem.Runtime.Serialization.Json.DataContractJsonSerializer(myPerson.GetType());

MemoryStream ms = new MemoryStream();

serializer.WriteObject(ms, myPerson);

string json = System.Text.Encoding.UTF8.GetString(ms.ToArray());

Response.Clear();

Response.ContentType = "application/json;charset=utf-8";

Response.Write(json);

Response.End();

We have seen people asking interview questions on REST after JSON questions. Below is a simple .NET interview question video with answer which talks about what is REST ?.

Also read this interesting ASP.NET interview question with answer on Server.Transfer and Response.Redirect ?

ASP.NET JSON interview question: - How to send JSON format from ASP.NET?Where stories live. Discover now