码迷,mamicode.com
首页 > Web开发 > 详细

ASP.NET与json对象互转

时间:2018-05-19 00:10:07      阅读:434      评论:0      收藏:0      [点我收藏+]

标签:void   charset   alt   rgs   color   获取   数据   程序包   解析   

这两天写这个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对象互转

标签:void   charset   alt   rgs   color   获取   数据   程序包   解析   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!