parse json to object c# code example
Example 1: object to json c#
using Newtonsoft.Json;
var jsonString = JsonConvert.SerializeObject(obj);
Example 2: c# parse json
Product product = new Product();
product.Name = "Apple";
product.Expiry = new DateTime(2008, 12, 28);
product.Price = 3.99M;
product.Sizes = new string[] { "Small", "Medium", "Large" };
string json = JsonConvert.SerializeObject(product);
Product deserializedProduct = JsonConvert.DeserializeObject<Product>(json);
Example 3: c# convert object to json
var jsonString = JsonConvert.SerializeObject(ObjectModel);
Example 4: js object to c# object
var obj = {
id: 0 ,
userName: 'Bill'
};
public class myClass
{
public int id;
public string userName;
}
$.ajax({
type: "POST",
url: '<your server url>' + 'DeserializeObject',
data: obj,
contentType: "application/text; charset=utf-8",
dataType: "text"
})
[HttpPost]
public void DeserializeObject(string objJson)
{
var obj = (new JavascriptSerializer()).Deserialize<myClass>(objJson);
}