📜  js 对象到 c# 对象 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:51.738000             🧑  作者: Mango

代码示例1
// 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: '' + '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(objJson);
}