Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A
dotnetinter

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

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 = new System.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 ?
Tags: .net interview questions and answers, asp.net interview question with answers, asp.net json interview question and, emit json using asp.net, information technology questions, rest interview questions
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments