`
webcode
  • 浏览: 5949760 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

asp.net序列化泛型集合

 
阅读更多

一般的Json+jquery+asp.net 的应用在项目中都是ajax的一部分,包括数据源和数据处理页面。经过在网络上的学习,为了可以方便的生成Json,网上一般的方法是引用了Newtonsoft.Json类来做此事。应用该类可以方便的生成Json数据串。

1.生成Json数据串

方法1,使用JavaScriptConvert.DeserializeObject对实体进行转换

下面这个是JavaScriptConvert.DeserializeObject的方法说明

//把Json字符串反序列化为对象
目标对象 = JavaScriptConvert.DeserializeObject(JSON字符串, typeof(目标对象));

view plaincopy to clipboardprint?
// 获取用户数据
IList<MUsersInfo> userInfoList = bllUser.GetSearch(userName, unitID, 0, 0);

// 使用方法对泛型实体进行转换
string jsonString = JavaScriptConvert.SerializeObject(userInfoList);

// 输出串
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/json";
Response.Write(jsonString);
Response.Flush();
Response.End();
// 获取用户数据
IList<MUsersInfo> userInfoList = bllUser.GetSearch(userName, unitID, 0, 0);

// 使用方法对泛型实体进行转换
string jsonString = JavaScriptConvert.SerializeObject(userInfoList);

// 输出串
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/json";
Response.Write(jsonString);
Response.Flush();
Response.End();

方法二,没有实体的情况下,可以适应这种

view plaincopy to clipboardprint?
// 获取用户数据
IList<MUsersInfo> userInfoList = bllUser.GetSearch(userName, unitID, 0, 0);

// 构成json数据串
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonWriter(sw);
foreach (MUsersInfo item in userInfoList)
{
writer.WriteStartArray();
writer.WriteStartObject();
writer.WritePropertyName("UserId"); // 属性名称
writer.Writue(item.Ts_Users_ID); // 属性值
writer.WritePropertyName("RealName");
writer.Writue(item.Ts_User_RealName);
writer.WriteEndObject();
writer.WriteEndArray();
}

writer.Flush();

// 生成串
string jsonString = sw.GetStringBuilder().ToString();

// 输出
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/json";
Response.Write(jsonString);
Response.Flush();
Response.End();
// 获取用户数据
IList<MUsersInfo> userInfoList = bllUser.GetSearch(userName, unitID, 0, 0);

// 构成json数据串
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonWriter(sw);
foreach (MUsersInfo item in userInfoList)
{
writer.WriteStartArray();
writer.WriteStartObject();
writer.WritePropertyName("UserId"); // 属性名称
writer.Writue(item.Ts_Users_ID); // 属性值
writer.WritePropertyName("RealName");
writer.Writue(item.Ts_User_RealName);
writer.WriteEndObject();
writer.WriteEndArray();
}

writer.Flush();

// 生成串
string jsonString = sw.GetStringBuilder().ToString();

// 输出
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/json";
Response.Write(jsonString);
Response.Flush();
Response.End();

两种方式各有各的好处,第一种方便,第二种灵活。

2.jquery读取json串

view plaincopy to clipboardprint?
$(document).ready(function() {
$("#Button1").click(function() {
$.getJSON("webService/UserList.aspx", function(json) {
alert("total " + json.length.toString() + " records");
for (var i = 0; i < json.length; i++) {
$("<li />").appendTo("div").html(json[i].Ts_User_RealName);
} 
});
});
});
$(document).ready(function() {
$("#Button1").click(function() {
$.getJSON("webService/UserList.aspx", function(json) {
alert("total " + json.length.toString() + " records");
for (var i = 0; i < json.length; i++) {
$("<li />").appendTo("div").html(json[i].Ts_User_RealName);
} 
});
});
});

分享到:
评论

相关推荐

    asp.net知识库

    泛型的序列化问题 .NET 2.0 泛型在实际开发中的一次小应用 C#2.0 Singleton 的实现 .Net Framwork 强类型设计实践 通过反射调用類的方法,屬性,字段,索引器(2種方法) ASP.NET: State Server Gems 完整的动态加载/卸载...

    《影院售票系统》编程语言为:ASP.Net

    泛型集合的使用 XML的读取 TreeView的绑定 使用虚方法实现多态 使用接口技术 使用序列化与反序列化

    C#实现json的序列化和反序列化实例代码

    两种方法都有例子,第一种是用泛型集合来存储的对象,然后将集合序列化一下;第二种是直接序列化的一个对象 代码如下:using System;using System.Collections.Generic;using System.Web.Script.Serialization;using ...

    精通C# 5.0和.NET 4.5框架应用开发 英文版

    工作与I / O服务和对象序列化21。第一部分:ADO.NET连接层22。ADO.NET第二部分:断开层23。第三部分:ADO.NET实体框架24。LINQ到XML 25。Windows通信基础26。Windows Workflow Foundation的27。介绍WPF和XAML 28。...

    C#与.NET3.5高级程序设计 part03

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part02

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part04

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part18 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part14 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part17 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part11 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part16 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part10 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part07 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part13 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part12 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part08 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part05 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

    C#与.NET3.5高级程序设计 part09 rar

    第21章 对象序列化.pdf 第22章 ADO.NET之一:连接层.pdf 第23章 ADO.NET之二:断开连接层.pdf 第24章 LINQ API编程.pdf 第25章 WCF.pdf 第26章 WF.pdf 第27章 WindowscForms编程.pdf 第28章 WPF和XAML.pdf ...

Global site tag (gtag.js) - Google Analytics