ASP.NET与json对象互转
这两天写这个xml跟json的读写,心累啊,也不是很理解,请大家多指教
首先来个热身菜做一个简单的解析json
在script里写一个简单的弹窗效果
<script>
//script里简单的解析json
var json = '{"name": "学生","info": [{ "count": "1", "stuname": "张三 ", "stuNO": "123" }, { "count": "2", "stuname": "里斯 ", "stuNO": "456" }] }'
var obj = JSON.parse(json);
alert(obj.name);
alert(obj.info[].count);//按顺序弹出消息弹框
alert(obj.info[].stuname);
</script>
效果如图

注意:在进行asp.net与json转换时,要首先安装一个json转化工具
项目—管理NuGet程序包—打开之后如图所示操作

工具包装好后要记得引用using Newtonsoft.Json;
案例
新建两个学生类Student.cs,StuList.cs和一个web窗体WebForm.aspx

1.Student.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace asp.net解析json
{
public class Student
{
public string StuNO { get; set; }
public string StuName { get; set; }
public Student()
{ }
public Student(string StuNO, string StuName)
{
this.StuNO = StuNO;
this.StuName = StuName;
}
}
}
2.StuList.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace asp.net解析json
{
public class StuList
{
public int count;
public List<Student> data;
public StuList()
{ }
}
}
3.WebForm.aspx代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="asp.net解析json.WebForm1" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title> </head>
<body>
<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<div>
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="对象转json" />
<asp:Button ID="Button2" runat="server" Text="json转对象" OnClick="Button2_Click" />
</form>
</body>
</html>
4.WebForm.aspx.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Newtonsoft.Json;//要记得引用
namespace asp.net解析json
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ } protected void Button1_Click(object sender, EventArgs e)
{
Student zhangsan = new Student("","张三");
Student lisi = new Student("","李四"); List<Student> stulist = new List<Student>();//存储在集合里
stulist.Add(zhangsan);
stulist.Add(lisi); StuList stuList = new StuList();
stuList.count = stulist.Count;
stuList.data = stulist;
string json = JsonConvert.SerializeObject(stuList);
ViewState["json"] = json;//获取你保存在网页里的信息
Label1.Text = json;
} protected void Button2_Click(object sender, EventArgs e)
{
string json = ViewState["json"].ToString();
StuList stu = JsonConvert.DeserializeObject<StuList>(json);
for (int i = ; i < stu.count; i++)//遍历集合里的数据
{
string info = "学号:" + stu.data[i].StuNO + "姓名:" + stu.data[i].StuName + "<hr />";
Label2.Text += info;
}
}
}
}
测试结果

game over
ASP.NET与json对象互转的更多相关文章
- Delphi中JSon SuperObject 使用:数据集与JSON对象互转
在delphi中,数据集是最常用数据存取方式.因此,必须建立JSON与TDataSet之间的互转关系,实现数据之间通讯与转换.值得注意的是,这只是普通的TDataset与JSON之间转换,由于CDS包 ...
- JSON字符串 与 JSON对象 互转
一,JSON字符串与JSON对象的区别 JSON对象是符合JSON格式的对象,可以用"对象.属性"进行存取值; JSON字符串是符合JSON格式的字符串; 二,JSON字符串-&g ...
- JavaScript 字符串与json对象互转的几种方法
第一种:浏览器支持的转换方式(Firefox,chrome,opera,safari,ie)等浏览器: JSON.parse(jsonstr); //可以将json字符串转换成json对象 JSON. ...
- 字符串和JSON对象互转的方法
采用Ajax的项目开发过程中,经常需要将JSON格式的字符串返回到前端,前端解析成JS对象(JSON ).字符串转JSON对象 1.eval方式解析.function strToJson(str){ ...
- JSON 对象互转
以前写过用反射,转换,后来觉得有很大漏洞,最近发现有人写过这个help类,所以保存下来 public class JSONHelper { /// <summary> /// DataRo ...
- Json与Java对象互转之Gson学习
Json与Java对象互转之Gson学习 请尊重他人的劳动成果.转载请注明出处:Json与Java对象互转之Gson学习 我曾在<XML,Object,Json转换之浅析Xstr ...
- 解决ASP.NET Web API Json对象循环参考错误
前言 一般我们在开法 ASP.NET Web API 时,如果是使用 Entity Framework 技术来操作数据库的话,当两个 Entity 之间包含导览属性(Navigation Proper ...
- 序列化json对象,通过ajax传入asp.net mvc后台
序列化json对象,通过ajax传入asp.net mvc后台 序列化json对象,通过ajax传入asp.net mvc后台 今天遇到一个问题,准备把组织好的json对象通过jquery.aja ...
- javascript中json对象json数组json字符串互转及取值
今天用到了json数组和json对象和json类型字符串之间互转及取值,记录一下: 1.json类型的字符串转换为json对象及取值 var jsonString = '{"bar" ...
随机推荐
- HashMap的容量大小增长原理(JDK1.6/1.7/1.8)
. 前言 HashMap的容量大小会根据其存储数据的数量多少而自动扩充,即当HashMap存储数据的数量到达一个阈值(threshold)时,再往里面增加数据,便可能会扩充HashMap的容量. 可能 ...
- phpSpreadSheet 中 使用的 一些坑
如果是upupw,它 做了 安全限制...将 上传目录 写成 uploadfiles 等 才能 写进去.. 文件路径 也不要有 中文..很有可以能 下载时 找不到路径....这个太坑...
- POJ 3067 Japan (树状数组 && 控制变量)
题意: 西海岸和东海岸有分别有n (1~n)个和m (1~m)个城市, 两个海岸的城市之间有k条公路连通, 公路会相交, 现在给出城市和公路的信息问你由这些公路组成的复杂交通有多少个交点 (如果两个条 ...
- Java中Array与ArrayList的主要区别
1)精辟阐述: 可以将 ArrayList想象成一种"会自动扩增容量的Array". 2)Array([]):最高效:但是其容量固定且无法动态改变: ArrayList: ...
- java/Android String.split 字符串分割
特殊符号分割时需加[].如下图
- java中double的四舍五入 BigDecimal
转载:https://blog.csdn.net/xiaobing_122613/article/details/71077225 1. 功能 将程序中的double值精确到小数点后两位.可以四舍五入 ...
- Jmeter录制pc脚本
1.打开jmeter后可以看到左边窗口有个“测试计划”和“工作台”,右键“测试计划”,添加 Threads(Users) →线程组,再右键 线程组→添加 配置元件→Http请求默认值 Http请求默认 ...
- PCB的版本控制
http://club.szlcsc.com/article/details_1783_1.html 转载自:http://www.amobbs.com/thread-5606014-1-1.html ...
- 【Linux】tcp缓冲区大小的默认值、最大值
Author:阿冬哥 Created:2013-4-17 Blog:http://blog.csdn.net/c359719435/ Copyright 2013 阿冬哥 http://blog.cs ...
- firefox插件-自动化测试工具-selenium IDE
教程:http://www.yiibai.com/selenium/selenium_download_ide.html 下载地址:https://addons.mozilla.org/en-US/f ...