javascript object to c# code example

Example: js object to c# object

// Construct a object in JS

var obj = {
  id: 0 ,
  userName: 'Bill'
};

// C# class:

public class myClass
{
  public int id;
  public string userName;
}

// Then for example using AJAX send your data to C#,
// and deserialize, when you need work with object

$.ajax({
	type: "POST",
  	// Note that you need to pass the method as url
	url: '<your server url>' + 'DeserializeObject',
  	// your data is the object you want to send
	data: obj,
	contentType: "application/text; charset=utf-8",
	dataType: "text"
})

[HttpPost]
public void DeserializeObject(string objJson)
{
var obj = (new JavascriptSerializer()).Deserialize<myClass>(objJson);
}