Archive for the ‘Json’ Category

namespace javascriptserializer could not be found

JavaScriptSerializer is situated in System.Web.Extensions Assembly. You should add it to your project references using add reference option.

Advertisements

Make JSON data using JavaScriptSerializer in asp.net

using System.Net;
using System.Text;
using System.IO;
using System.Data;
using System.Web.Script.Serialization;

string json = new JavaScriptSerializer().Serialize(new
{
empid = eid,
empname = ename,
empsalary = salary
});

Send Json Data to Webservice and get response from Webservice

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Text;
using System.IO;
using System.Data;
using System.Web.Script.Serialization;

 

string url =”http://abc.com”;

var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = “application/json”;
httpWebRequest.Method = “POST”;

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
empid = “1001”,
empname = “test name”,
empsalary= “10000”
});

streamWriter.Write(json);
}

var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}