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" ...
随机推荐
- Java 标准IO高可用类
输出: 使用: * <pre> * public class TestStdOut { * public static void main(String[] args) { * int a ...
- Canvas制作动态进度加载水球
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂
题目传送门 题目描述:给出n,x,mod.求s[n]. s[n]=s[n-1]+(x^n)*(n^x)%mod; 思路:这道题是hdu5950的进阶版.大家可以看这篇博客hdu5950题解. 由于n很 ...
- Java缓存类loadingCache
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artif ...
- SpringMVC什么时候配置 视图解析器
当Action返回的是一个真实路径的时候,视图解析器可不进行配置 当Action返回的是逻辑路径的时候,我们必须要在配置文件中注册视图解析器并为该逻辑路径添加前缀和后缀
- Access restriction: The type Base64 is not accessible due to restriction on
java build path>把libraries中的JRE System Library删除重新导入.
- Robot Framework搭建
需要安装的内容如下: 1. Python2.7.13(听说python3对RF支持的不是很好,所以我下的Python2) 2. wxPython 2.8.12.1(只能这个版本) 3. robotfr ...
- 电感的Q值
电感的Q值又称为品质因数,即在通过一定频率信号时,感抗与等效损耗之比.品质因数越高即系统损耗越小效率越高,一般为50`100,最高500左右,再大就会烧毁.一般Q值与很多因素有关:绕线粗细,长度与直径 ...
- jquery dataTable 自定义 Button及按钮事件
参考网址:http://stackoverflow.com/questions/18134913/jquery-datatabletabletool-custom-buttons-calling-ev ...
- [转]jquery.pagination.js分页
本文转自:http://www.cnblogs.com/knowledgesea/archive/2013/01/03/2841554.html 序言 这一款js分页使用起来很爽,自己经常用,做项目时 ...