2022年2月26日 星期六

[研究][ASP.NET WebForm C#]Newtonsoft.Json 13.0.1 序列化、反序列化測試

[研究][ASP.NET WebForm C#]Newtonsoft.Json 13.0.1 序列化、反序列化測試

2022-02-26

續這篇

[研究][ASP.NET WebForm C#]HttpClient 呼叫 Web API、REST/RESTful API 測試(二)
https://shaurong.blogspot.com/2022/02/aspnet-webform-chttpclient-web_26.html

Visual Studio2022 v17.1.0 + ASP.NET + C# + Web Application + WebForm

Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" 
CodeBehind="Default.aspx.cs" Inherits="WebApplication4.Default" %>

<!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">
        <div>
        </div>
    </form>
    <asp:Label ID="Label_MSG1" runat="server"></asp:Label><br />
    <asp:Label ID="Label_MSG2" runat="server"></asp:Label><br />
    <asp:Label ID="Label_MSG3" runat="server"></asp:Label><br />
</body>
</html>

Default.aspx.cs


using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace WebApplication4
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            JSONTest();
        }
        public class Student
        {
            public int ID { get; set; }
            public string CName { get; set; }
            public int Age { get; set; }
            public string Sex { get; set; }
        }
        
        protected void JSONTest()
        {
            List<Student> listStudentSample = new List<Student>()
            {
                new Student(){ID=1,CName="John",Age=21,Sex="男"},
                new Student(){ID=2,CName="Mary",Age=29,Sex="女"}
            };

            //Newtonsoft.Json序列化
            string jsonData = JsonConvert.SerializeObject(listStudentSample);
            Label_MSG1.Text= jsonData;

            foreach(Student student in listStudentSample)
            {
                Label_MSG1.Text = Label_MSG1.Text + "<br />" 
                    + JsonConvert.SerializeObject(student);//序列化
            }
            Label_MSG1.Text = Label_MSG1.Text +  "<br />----------<br />";

            //Newtonsoft.Json反序列化
            string json = @"{ 'CName':'Alice','Age':'30','ID':'1','Sex':'女'}";
            Label_MSG2.Text = json;
            Student stu = JsonConvert.DeserializeObject<Student>(json);//反序列化
            Label_MSG3.Text = stu.CName;
        }
    }
}

但目前 class Student 中資料型態都是基本型態,不是 Class;

Newtonsoft.Json 沒有直接支援巢狀反序列化。

(完)

相關

RestSharp Next (v107) | RestSharp
https://restsharp.dev/v107/#restsharp-v107

NuGet Gallery | RestSharp
https://www.nuget.org/packages/RestSharp/

RestSharp - Simple .NET REST Client - GitHub
https://github.com/restsharp/RestSharp

[研究][ASP.NET WebForm C#]Newtonsoft.Json 13.0.1 序列化、反序列化測試https://shaurong.blogspot.com/2022/02/aspnet-webform-cnewtonsoftjson-1301.html

[研究][ASP.NET WebForm C#]HttpClient 呼叫 Web API、REST/RESTful API 測試(二)
https://shaurong.blogspot.com/2022/02/aspnet-webform-chttpclient-web_26.html

[研究][ASP.NET WebForm C#]HttpClient 呼叫 Web API、REST/RESTful API 測試(一)
http://shaurong.blogspot.com/2022/02/aspnet-webform-chttpclient-web.html

[研究][ASP.NET WebForm C#]WebClient 呼叫 Web API、REST/RESTful API 測試https://shaurong.blogspot.com/2022/02/aspnet-webform-cwebclient-web.html


沒有留言:

張貼留言