使用C#中JavaScriptSerializer类将对象转换为Json格式数据
将对象转换为json格式字符串:
private JavaScriptSerializer serializer = new JavaScriptSerializer();
protected void Page_Load(object sender, EventArgs e)
{
Books book = new Books();
book.BookId = ;
book.BookName = "书籍1";
book.BookCount = ;
string jsonStr = serializer.Serialize(book);
//jsonStr结果:{"BookId":1,"BookName":"书籍1","BookCount":999}
}
Aspx前台页面,jQuery发送Ajax请求:
function loadrecomticket() {
jQuery.post(
"/Ajax/TicketBoxAsx.ashx",
{
ajaxMethod: "getrecomticket",
random: Math.random()
},
function(data) {
if (data.result) { //面向对象思想:把返回的对象data,直接data.result,类似对象.属性名
$("#M1_LeftCount").html(data.list[0].leftCount);
$("#M1_GetCount").html(data.list[0].GetCount);
$("#M_UsedCount").html(data.list[0].UsedCount);
$("#M2_LeftCount").html(data.list[1].LeftCount);
$("#M2_GetCount").html(data.list[1].GetCount);
$("#M2_UsedCount0").html(data.list[1].UsedCount);
$("#M3_LeftCount").html(data.list[2].LeftCount);
$("#M3_Getcount").html(data.list[2].GetCount);
$("#M3_UsedCount").html(data.list[2].UsedCount);
}
},
"json");
}
一般处理程序接收参数并处理请求,返回json格式数据:
namespace Test.Ajax
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TicketBoxAsx : BaseCommon, IHttpHandler
{
private Business.RecomTicket recomTicket = new Business.RecomTicket(); public void ProcessRequest(HttpContext context)
{
if (this.UserId > )
{
if (!string.IsNullOrEmpty(context.Request["ajaxMethod"]))
{
context.Response.ContentType = "text/plain";
string ajaxMethod = context.Request["ajaxMethod"].ToLower();
switch (ajaxMethod)
{
case "getrecomticket":
GetRecomTicket(context);
break;
}
}
}
else
{
Utility.ResponseWriteEnd(this.ProcessResponseText("({result:0,error:'请先登录!'})"));
}
} public bool IsReusable
{
get
{
return false;
}
} private void GetRecomTicket(HttpContext context)
{
TicketBoxInfo M1 = new TicketBoxInfo(); //创建3个对象
TicketBoxInfo M2 = new TicketBoxInfo();
TicketBoxInfo M3 = new TicketBoxInfo();
BusinessResult<RecomTicketInfo> info = this.recomTicket.GetUserVoteTicket(this.UserId);
if (info.IsSuccess)
{
RecomTicketInfo recomTicketInfo = info.ReturnObject;
//为3个对象属性赋值
M1.leftCount = Math.Max(recomTicketInfo.MaxRecomTicket - recomTicketInfo.TodayUsedRecomTicket, );
M1.MGetCount = recomTicketInfo.MaxRecomTicket;
M1.UsedCount = recomTicketInfo.TodayUsedRecomTicket;
M2.leftCount = Math.Max(recomTicketInfo.MaxRecomTicket - recomTicketInfo.TodayMMUsedRecomTicket, );
M2.MGetCount = recomTicketInfo.MaxRecomTicket;
M2.UsedCount = recomTicketInfo.TodayMMUsedRecomTicket;
M3.leftCount = Math.Max(recomTicketInfo.MaxRecomTicket - recomTicketInfo.TodayWXUsedRecomTicket, );
M3.MGetCount = recomTicketInfo.MaxRecomTicket;
M3.UsedCount = recomTicketInfo.TodayWXUsedRecomTicket; }
JavaScriptSerializer js = new JavaScriptSerializer(); //命名空间:using System.Web.Script.Serialization;
Utility.ResponseWriteEnd(this.ProcessResponseText("({result:1, list:[" + js.Serialize(M1) + "," + js.Serialize(M2) + "," + js.Serialize(M3) + "]})"));
} }
}
备注:
JSON格式:
普通形式:
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" } 数组形式:
.单元素:
{
"people": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb"},
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
]
} .多元素:
{ "programmers": [
{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
] } 格式应用
掌握了 JSON 格式之后,在 JavaScript 中使用它就很简单了。JSON 是 JavaScript 原生格式,这意味着在 JavaScript 中处理 JSON 数据不需要任何特殊的 API 或工具包。
赋值给变量
例如,可以创建一个新的 JavaScript 变量,然后将 JSON 格式的数据字符串直接赋值给它: var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },
{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },
{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }
],
"authors": [
{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },
{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },
{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }
],
"musicians": [
{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },
{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }
数据访问:
people.authors[].genre // Value is "fantasy" 下标从0开始
people.musicians[].lastName // Undefined. This refers to the fourth entry, and there isn't one
people.programmers[].firstName // Value is "Elliotte"
博文推荐:C# JSON字符串序列化与反序列化
格式应用
赋值给变量
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
var people = { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }],"authors": [{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }],"musicians": [{ "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },{ "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" } |
使用C#中JavaScriptSerializer类将对象转换为Json格式数据的更多相关文章
- 使用Javascript/jQuery将javascript对象转换为json格式数据 - 海涛的CSDN博客 - 博客频道 - CSDN.NET
body{ font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI& ...
- ObjectMapper用于将java对象转换为json格式数据以及JSONObject对象解析json格式数据
ObjectMapper objectMapper = new ObjectMapper(); //反序列化的时候如果多了其他属性,不抛出异常 objectMapper.configure(Deser ...
- JSon_零基础_005_将po(bean)对象转换为JSon格式的对象字符串,返回给界面
将po(bean)对象转换为JSon格式的对象字符串,返回给界面 导入jar包: 编写po(bean)类: package com.west.webcourse.po; /** * 第01步:编写be ...
- JSon_零基础_004_将Set集合对象转换为JSon格式的对象字符串,返回给界面
将Set集合对象转换为JSon格式的对象字符串,返回给界面 需要导入的jar包: 编写:servlet: package com.west.webcourse.servlet; import java ...
- JSon_零基础_003_将Map集合对象转换为JSon格式的对象字符串,返回给界面
将Map集合对象转换为JSon格式的对象字符串,返回给界面 需导入的jar包: 编写servlet: package com.west.webcourse.servlet; import java.i ...
- 对象转换为json格式,类似中间层API
<一头扎进SpringMvc视频教程\<一头扎进SpringMvc>第四讲 源码\> 对象自动转换为json格式要在 spring-mvc.xml添加一个东西 ,和对应的命名空 ...
- js对象转换为json格式时,js对象属性中有值为null和undefined注意事项
当属性值为null时: 当属性值为undefined时: 只有当属性值为未定义时, js对象转换成json格式时会忽略该属性.
- c# 将匿名类或者集合转Json格式数据一些方法
要说写这个功能呢也是因为工作需要,白天呢上班写个Web页面需要ajax请求后台并将数据以Json格式传会前端,由于公司特殊性吧,不能连外网(很苦比).所以只有等到晚上回家上网边查边写! public ...
- 在SQL中直接把查询结果转换为JSON数据
下面这篇中,已经有准备一些数据: <MS SQL server对象类型type>https://www.cnblogs.com/insus/p/10903916.html 为前端服务,直接 ...
随机推荐
- [Java] 使用Comparator排序对象
package test.collections; import java.util.ArrayList; import java.util.Collection; import java.util. ...
- angularjs之表达式
一:angularjs表达式的解析 angularjs会在运行$digest循环中自动解析表达式,但有时手动解析表达式也是非常用用的. angularjs通过$parse这个内部服务来进行表达式的运算 ...
- LeetCode 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- 使用U盘在X230上安装Mavericks/Win7-黑苹果之路
新笔记本x230,毫不犹豫继续开始黑苹果之路,这次当然是上最新版本了,谁知道这条道路真是曲折艰难啊,从年前开始,直到前天才算安装成功,还有一堆硬件没驱动上,现记录过程以备以后查看: 1.准备机器.本来 ...
- OC基础(8)
自定义代码段 实例变量修饰符 依赖关系 *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bott ...
- Appium小试
最近有空玩了一下Appium,记录一下 1.下载Appium for windows,现在是0.12.3版本 https://bitbucket.org/appium/appium.app/downl ...
- 洗清UI自动化鸡肋说的不白之冤
人类文明发展的一个重要标识是工具的诞生,当人类开始制作工具来提高生产力时,就逐渐拉开了与其他生物的距离.曾在2013年,<Google如何测试软件>中提到的分层自动化金字塔,轰动业界.而在 ...
- 初探接口测试框架--python系列6
点击标题下「蓝色微信名」可快速关注 坚持的是分享,搬运的是知识,图的是大家的进步,没有收费的培训,没有虚度的吹水,喜欢就关注.转发(免费帮助更多伙伴)等来交流,想了解的知识请留言,给你带来更多价值,是 ...
- 【Unity Shaders】学习笔记——SurfaceShader(十一)光照模型
[Unity Shaders]学习笔记——SurfaceShader(十一)光照模型 转载请注明出处:http://www.cnblogs.com/-867259206/p/5664792.html ...
- docker 1.12设置非https访问registry
升级docker到1.12后,发现使用原来的/etc/sysconfig/docker文件中设置--insecure-registry的方式,访问registry失败,提示"http: se ...