该篇文章是我于2009年6月10日通过自己编写的工具,批量从位于在博客园的博客站点(http://chenxizhang.cnblogs.com)同步而来。文章中的图片地址仍然是链接到博客园的。特此说明! 陈希章原文地址:http://www.cnblogs.com/chenxizhang/archive/2009/05/28/1491238.html原文标题:Json.NET 原文发表:2009/5/28 1:55:00 |
简而言之,这是一个可以用于.NET的Json辅助工具类。它可以将对对象序列化为json字符串。下面是一个我自己写的用在ashx中的例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Data;
namespace WebApplication1
{
///
/// $codebehindclassname$ 的摘要说明
///
[WebService(Namespace = " http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class GetData : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//var Employee = new { EmployeeName = "chenxizhang",Age = "20" };//直接构造一个简单的对象
NorthwindDataContext db = new NorthwindDataContext();
var orders = db.Orders.Where(o => o.OrderID <= 10250);//这是取得一系列对象
JsonSerializerSettings settings = new JsonSerializerSettings();
settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
string result = JsonConvert.SerializeObject(new { Orders = orders },Formatting.Indented,settings);//需要注意的是,如果返回的是一个集合,那么还要在它的上面再封装一个类。否则客户端收到会出错的。
context.Response.Write(result);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
在页面中使用jquery调用的方式如下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jqueryGetOrdersbyajax.aspx.cs" Inherits="WebApplication1.jqueryGetOrdersbyajax" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">
<% = minjquery %>
value=" < " /> value=" >> " />
订单ID |
客户ID |
雇员ID |
订购日期 |
发货日期 |
货主名称 |
货主地址 |
货主城市 |
更多信息 |
---|---|---|---|---|---|---|---|---|
LOADING....
其官方的资料如下,请参考
http://james.newtonking.com/projects/json-net.aspx
The Json.NET library makes working with JavaScript and JSON formatted data in .NET simple. Quickly read and write JSON using the JsonReader and JsonWriter or serialize your .NET objects with a single method call using the JsonSerializer.
Features
- LINQ to JSON
- Lightning fast JsonReader and JsonWriter
- The JsonSerializer for quickly converting your .NET objects to JSON and back again
- Json.NET can optionally produce well formatted,indented JSON for debugging or display
- Attributes like JsonIgnore and JsonProperty can be added to a class to customize how a class is serialized
- Ability to convert JSON to and from XML
- Supports multiple platforms: .NET,Silverlight and the Compact Framework
Example
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);
//{
// "Name": "Apple",
// "Expiry": new Date(1230422400000),
// "Price": 3.99,
// "Sizes": [
// "Small",
// "Medium",
// "Large"
// ]
//}
Product deserializedProduct = JsonConvert.DeserializeObject(json);
Documentation
Json.NET - Quick Starts & API Documentation
History
Json.NET grew out of projects I was working on in late 2005 involving JavaScript,AJAX and .NET. At the time there were no libraries for working with JavaScript in .NET so I began to grow my own.
Starting out as a couple of static methods for escaping JavaScript strings,Json.NET evolved as features were added. To add support for reading JSON a major refactor was required and Json.NET will split into the three major classes it still uses today,JsonReader,JsonWriter and JsonSerializer.
Json.NET was first released in June 2006. Since then Json.NET has been downloaded thousands of times by developers and is used in a number of major projects open source projects such as MonoRail,Castle Project's MVC web framework,and Mono,an open source implementation of the .NET framework.
作者:陈希章 出处:http://blog.csdn.net/chen_xizhang 本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 |
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。