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" ...
随机推荐
- 让函数的input、output更"函数化"
前言 我们都知道函数的基本形式为:output f(input),且先按这种形式进行input与output的分析,我们的input与output可以有更好的设计方式,而我们的output是选择使用r ...
- Experimental Educational Round: VolBIT Formulas Blitz F
Description One company of IT City decided to create a group of innovative developments consisting f ...
- linux / OS 杀死进程
1,查询端口 sudo netstat -apn | grep 端口号 2,杀死进程kill -9 应用程序进程id
- GC:并行回收CMS详解
CMS详解 https://www.cnblogs.com/ggjucheng/p/3977612.html CMS默认不回收Perm, 需要加参数 +CMSPermGenSweepingEnable ...
- 转 使用utl_http获取某个http页面内容
#########1.ACL详细解释: 11g 对于XDB UTL_HTTP or others package 的权限管控进一步加强,如果需要使用到XDB 以下包 UTL_TCP, UTL_SMT ...
- springMVC初探视图解析器——InternalResourceViewResolver
springmvc在处理器方法中通常返回的是逻辑视图,如何定位到真正的页面,就需要通过视图解析器. springmvc里提供了多个视图解析器,InternalResourceViewResolver就 ...
- Unity 在Inspector面板,脚本前面的激活对勾
写个脚本,把它挂在一个游戏对象上: using System.Collections; using System.Collections.Generic; using UnityEngine; pub ...
- 性能测试工具LoadRunner07-LR之Virtual User Generator 参数化设置
1.Select next row[选择下一行]: 顺序(Sequential):按照参数化的数据顺序,一个一个的取 随机(Random):参数化中的数据,每次随机的从中抽取数据 唯一(Unique) ...
- 深入学习webpack(四)
更多内容可以看此文档.
- pat1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...