Json.Net Demo2
新建一个空的Web项目,名称JsonServer,该网页实现Ajax数据请求和响应。
添加Newtonsoft.Json.dll的Dll引用,添加JQuery API文件,目录结构如下:
新建一个Person类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
///Person 的摘要说明
/// </summary>
/// <summary>
/// 包含用户的基本信息
/// </summary>
public class Person
{
/// <summary>
/// 获取或设置用户名
/// </summary>
public string Name { get; set; } /// <summary>
/// 获取或设置用户年龄
/// </summary>
public int Age { get; set; } /// <summary>
/// 获取或设置用户性别
/// </summary>
public string Gender { get; set; } /// <summary>
/// 获取或设置用户国籍
/// </summary>
public string Country { get; set; } /// <summary>
/// 获取或设置用户电子邮箱
/// </summary>
public string Email { get; set; }
}
Person
新建一个数据操作类PersonRepository
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
///Class1 的摘要说明
/// </summary>
/// <summary>
/// 用户操作类
/// </summary>
public class PersonRepository
{
/// <summary>
/// 获取用户列表
/// </summary>
/// <returns>所有用户信息</returns>
public static List<Person> GetPersons()
{
List<Person> ps = new List<Person>();
Person p1 = new Person { Name = "Tom", Age = , Country = "US", Gender = "Male", Email = "tom@gmail.com" };
Person p2 = new Person { Name = "Jack", Age = , Country = "UK", Gender = "Male", Email = "jack@gmail.com" };
Person p3 = new Person { Name = "Eden", Age = , Country = "Canada", Gender = "Female", Email = "eden@gmail.com" };
Person p4 = new Person { Name = "Li Hua", Age = , Country = "China", Gender = "Male", Email = "lihui@163.com" };
Person p5 = new Person { Name = "Lvo", Age = , Country = "US", Gender = "Male", Email = "lvo@gmail.com" };
ps.Add(p1);
ps.Add(p2);
ps.Add(p3);
ps.Add(p4);
ps.Add(p5);
return ps;
}
}
PersonRepository
新建一个一般处理程序PersonHandler
<%@ WebHandler Language="C#" Class="PersonHandler" %> using System;
using System.Web;
using System.Collections.Generic;
using Newtonsoft.Json;
/// <summary>
/// 处理用户类的请求
/// </summary>
public class PersonHandler : IHttpHandler
{ public void ProcessRequest(HttpContext context)
{
List<Person> persons = PersonRepository.GetPersons();
string json = JsonConvert.SerializeObject(persons);
context.Response.Write(json);
} public bool IsReusable
{
get
{
return false;
}
}
}
PersonHandler
添加一个Demo.html页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body> <div>
<table border="">
<thead>
<tr>
<td>
用户名
</td>
<td>
年龄
</td>
<td>
性别
</td>
<td>
国籍
</td>
<td>
电子邮箱
</td>
</tr>
</thead>
<tbody id="personBody">
</tbody>
</table>
</div> <script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.getJSON("PersonHandler.ashx", function (data, status) {
if (status == "success") {
$.each(data, function (index, item) {
var beginTag = "<tr><td>";
var endTag = "</td></tr>";
var tag = "</td><td>";
$("#personBody").append($(beginTag + item.Name + tag + item.Age + tag + item.Gender + tag
+ item.Country + tag + item.Email + endTag));
});
}
});
});
</script> </body>
</html>
demo.htm
运行程序,在浏览器中查看Demo.html页面:
Json.Net Demo2的更多相关文章
- webpack02
consumer-index.html <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- angularJS入门小Demo2 【包含不用数据库而用data.json格式响应前台的ajax请求方式测试】
事件绑定: <html> <head> <title>angularJS入门小demo-5 事件指令</title> <script src=&q ...
- js中eval详解,用Js的eval解析JSON中的注意点
先来说eval的用法,内容比较简单,熟悉的可以跳过eval函数接收一个参数s,如果s不是字符串,则直接返回s.否则执行s语句.如果s语句执行结果是一个值,则返回此值,否则返回undefined. 需要 ...
- Python全栈--7模块--random os sys time datetime hashlib pickle json requests xml
模块分为三种: 自定义模块 内置模块 开源模块 一.安装第三方模块 # python 安装第三方模块 # 加入环境变量 : 右键计算机---属性---高级设置---环境变量---path--分号+py ...
- jQuery form插件的使用--处理server返回的JSON, XML,HTML数据
详细代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...
- http://www.bejson.com/go.html?u=http://www.bejson.com/demo2.html
json 解析工具http://www.bejson.com/go.html?u=http://www.bejson.com/demo2.html
- 通过Jquery中Ajax获取json文件数据
1. JSON(JavaScript Object Notation): javaScript对象表示法: 是存储和交换文本信息的语法,比xml更小,更快,更易解析. 2. JSON基本书写格式 : ...
- CI笔记6 json 传值
CI3.x 使用json,配合easyui, 其实很简单,走了很多的弯路, 首先在ci的控制器重,建立2个方法,一个用于显示加载view,一个用于echo json,就可以了. 需要注意的是,在ci的 ...
- C# Json反序列化处理
最近换工作了 从客户端转到Web端 第一个任务就是去别人的页面上抓取数据 用到的是JSON 因为他们网站json的格式有点怪 所以 就在JSON反序列化上面 花了一点时间 首先用到的工具是http:/ ...
随机推荐
- HDR Defered Shading (using MRT)
http://http.download.nvidia.com/developer/SDK/Individual_Samples/DEMOS/Direct3D9/DeferredShading.zip ...
- 【转载】Linux系统与性能监控
原文地址:http://kerrigan.sinaapp.com/post-7.html Linux System and Performance Monitoring http://www.hous ...
- 256 terabytes random-access memory
Computer Systems A Programmer's Perspective Second Edition As we will discuss, the extension of IA32 ...
- PureBasic 集成Form设计器的使用
The PureBasic IDE has a very powerful integrated form designer, which allows to design easily window ...
- Delphi如何打开DBF数据库
Delphi语言,无论Delphi7.Delphi2007或者Delphi XE2或3,无需安装其它附加的部件,就可以实现DBF文件的打开及相关操作,网络上很多要用到什么ADO引擎的,其实未必,只有安 ...
- android文字阴影效果(转)
关于android文字阴影,共有四个属性可以设置: android:shadowColor :阴影颜色 android:shadowDx :阴影x方向位移 android:shadowDy :阴影y方 ...
- [daily][network] NAT原理(转)
写在转发之前: 一直以来,我一直有一个疑惑,SNAT的时候,如果两个内网主机恰巧使用了相同的源端口号该怎么办呢? 我自己猜测的方法是改掉一个端口号,把端口一起映射(当然还有另一个设想,就是把包同时广播 ...
- java开发bug 在启动Tomcat 6.0时发现第一条信息便是
MyEclipse 8.5 + tomcat6 + jdk 1.8 启动的时候报错: The APR based Apache Tomcat Native library which allows o ...
- 蓝牙的L2CAP协议
1.概述 L2CAP能向上层提供面向连接的或者无连接的数据服务,拥有multiplexing capability and segmentation and reassembly operat ...
- Apache的HBase与cdh的hue集成(不建议不同版本之间的集成)
1.修改hue的配置文件hue.ini [hbase] # Use full hostname with security. hbase_clusters=(Cluster|linux-hadoop3 ...