在ajax的已不请求中,常常返回json对象。可以利用json.net给我们提供的api达到快速开发。
例子:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Newtonsoft.Json;//这是json.net的命名空间
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
StudentInfo info = new StudentInfo();
info.Sname = "dld";
info.Gender = "女";
info.Age = 33;
string str = JsonConvert.SerializeObject(info);
Response.Write(str);
}
}
public class StudentInfo
{
public string Sname { get; set; }
public string Gender { get; set; }
public byte Age { get; set; }
}
。使用get方式
1.前台
//复杂json对象提交
var person = {'per':"{ 'id': 1, 'name': '张三', 'sex': '男' }"};
$.ajax({
type: "get",
url: "JsonObject.asmx/GetPersonByObject",
data: person,
dataType: 'json',
contentType: 'application/json;charset=utf-8',
success: function (data) {
if (data.d == "1") {
$("#hello").text("服务器接收成功!");
}
else {
$("#hello").text("服务器接收数据失败!");
}
},
error: function () {
$("#hello").text("程序运行出错!");
}
});
2.后台
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json, UseHttpGet=true)]
public string GetPersonByObject()
{
string jsonStr = HttpContext.Current.Request["per"];
Person per = jsonStr.JsonDeserialezer<Person>();//将json字符串反序列化
if (per.Id == 1)
{
return "1";
}
return "0";
}
二。使用post方式
1.前台
var person = "{'per':\"{ 'id': 1, 'name': '张三', 'sex': '男' }\"}";
$.ajax({
type: "post",
url: "JsonObject.asmx/GetPersonByObject",
data: person,
dataType: 'json',
contentType: 'application/json;charset=utf-8',
success: function (data) {
if (data.d == "1") {
$("#hello").text("服务器接收成功!");
}
else {
$("#hello").text("服务器接收数据失败!");
}
},
error: function () {
$("#hello").text("程序运行出错!");
}
});
2.后台
[WebMethod]
public string GetPersonByObject(string per)
{
Person person= per.JsonDeserialezer<Person>();//将json反序列化
if (person.Id == 1)
{
return "1";
}
return "0";
}
三。List类型json提交,post方式
1.前台
//复杂json对象提交2
var person = "{'per':\"[{ 'id': 1, 'name': '张三', 'sex': '男' },{ 'id': 2, 'name': '王芳', 'sex': '女' }]\"}";
$.ajax({
type: "post",
url: "JsonObject.asmx/GetPersonByOjects",
data: person,
dataType: 'json',
contentType: 'application/json;charset=utf-8',
success: function (data) {
$("#hello").text("就收前台数据人数:"+data.d);
},
error: function () {
$("#hello").text("程序运行出错!");
}
});
2.后台
[WebMethod]
public int GetPersonByOjects(string per)
{
List<Person> list = per.JsonDeserialezer<List<Person>>();//反序列化json字符串
return list.Count;
}
四。List类型json提交,get方式
1.前台
var person = {'per':"[{ 'id': 1, 'name': '张三', 'sex': '男' },{ 'id': 2, 'name': '王芳', 'sex': '女' }]"};
$.ajax({
type: "get",
url: "JsonObject.asmx/GetPersonByOjects",
data: person,
dataType: 'json',
contentType: 'application/json;charset=utf-8',
success: function (data) {
$("#hello").text("就收前台数据人数:"+data.d);
},
error: function () {
$("#hello").text("程序运行出错!");
}
});
2.后台
[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json,UseHttpGet=true)]
public int GetPersonByOjects()
{
string per = HttpContext.Current.Request["per"];
List<Person> list = per.JsonDeserialezer<List<Person>>();
return list.Count;
}
http://blog.163.com/m13864039250_1/blog/static/213865248201373105642827/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?