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:/ ...
随机推荐
- JavaScript编码规范[百度]
JavaScript编码规范 1 前言 2 代码风格 2.1 文件 2.2 结构 2.2.1 缩进 2.2.2 空格 2.2.3 换行 2.2.4 语句 2.3 命名 ...
- SwfUpload学习记录
参考资料: SWFUpload 2.5.0版 官方说明文档 中文翻译版 了解SWFUpload 多文件上传配置详解 WEB版一次选择多个文件进行批量上传(swfupload)的解决方案 jQuery轻 ...
- Maven问题总结:could not resolve archetype xxxxxxx from any of the configured repositories
错误提示 Eclipse中通过Archetype创建Maven项目时报错:Could not resolve archetype xxxxxxx from any of the configured ...
- PHP 错误与异常 笔记与总结(5)配置文件中与错误日志相关的选项 && 将错误记录到指定的文件中
[记录错误(生产环境)] php.ini: ① 开启 / 关闭 错误日志功能 log_errors = On ② 设置 log_errors 的最大字节数 log_errors_max_len = 其 ...
- PHP 设计模式 笔记与总结(10)数据对象映射模式 2
[例2]数据对象映射模式结合[工厂模式]和[注册模式]的使用. 入口文件 index.php: <?php define('BASEDIR',__DIR__); //定义根目录常量 includ ...
- 《编写可维护的 Javascript》读书笔记(附录 A 部分):Javascript 编码风格指南(1)原始值
记录一下比较有用的编码规范(该指南是基于 Java 语言编码规范和 Javascript 编程规范,同时结合作者 Nicholos Zakas 的个人经验和喜好). 一些关于格式(包括缩进.行的长度. ...
- pycharm 皮肤主题及个性化设置
1.设置IDE皮肤主题 File -> Settings -> IDE Settings -> Appearance -> Theme -> 选择“Alloy.IDEA ...
- Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入
Web 软件测试 Checklist 应用系列,第 1 部分: 数据输入 本文为系列文章"Web 软件测试 Checklist 应用系列"中的第一篇.该系列文章旨在阐述 Check ...
- RT-Thread的线程间同步
禁止系统调度上一节< 多线程导致的临界区问题>中由于 test1 线程被 test2 线程打断,才导致了我们没有得到预期的结果,我们一般可通过关闭中断和调度器上锁这两种简单的途径来禁止系统 ...
- MySQL并发调优和IO调优
一.myisam的IO调优1.myisam通常在每次写入后把索引的改变刷写到磁盘上.所以批处理通常会更快点.做到这点,可以通过LOCK TABLES,他可以把写入控制到对表解锁.还可以用delay_k ...