分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

ASP.NET与json对象互转

发布时间:2023-09-06 01:55责任编辑:熊小新关键词:.NETjsjson

这两天写这个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[0].count);//按顺序弹出消息弹框 ?????????alert(obj.info[1].stuname); ???</script>
View Code

效果如图

注意:在进行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("1001","张三"); ???????????Student lisi = new Student("1002","李四"); ???????????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 = 0; i < stu.count; i++)//遍历集合里的数据 ???????????{ ???????????????string info = "学号:" + stu.data[i].StuNO + "姓名:" + stu.data[i].StuName + "<hr />"; ???????????????Label2.Text += info; ???????????} ???????} ???}}
View Code

测试结果

game over

ASP.NET与json对象互转

原文地址:https://www.cnblogs.com/kalezhangtao/p/9058440.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved