• 大小: 59KB
    文件类型: .zip
    金币: 2
    下载: 1 次
    发布日期: 2021-06-11
  • 语言: C#
  • 标签: Json  

资源简介

Json的使用Demo,交你如何封装和解析使用Json,一看就懂,简单实用

资源截图

代码片段和文件信息

using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Windows.Forms;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.script.Serialization;

namespace Demo_Json
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender EventArgs e)
        {
            Person p = new Person();
            p.ID = 1;
            p.Name = “大神“;
            p.Age = 28;
            p.BirthDay = DateTime.Now.AddYears(-28);
            var json = new javascriptSerializer() { MaxJsonLength = int.MaxValue };
            string Encode = json.Serialize(p);

            Person Decode = json.Deserialize>(Encode);
        }

        private void button2_Click(object sender System.EventArgs e)
        {
            List> people = new List>();
            for (int i = 0; i < 5; i++)
            {
                Person tempP = new Person();
                tempP.Content = new List();
                tempP.ID = i + 1;
                tempP.Name = “Keal“ + i + 1;
                tempP.Age = i + 20;
                tempP.BirthDay = DateTime.Now.AddYears(-20 - i);

                DogInfo dogLing = new DogInfo();
                dogLing.Name = “LingLing“;
                dogLing.Owner = “keal“;
                tempP.Content.Add(dogLing);

                DogInfo dogHui = new DogInfo();
                dogHui.Name = “HuiHui“;
                dogHui.Owner = “keal“;
                tempP.Content.Add(dogHui);

                DogInfo dogLa = new DogInfo();
                dogLa.Name = “LaLa“;
                dogLa.Owner = “val“;
                tempP.Content.Add(dogLa);

                people.Add(tempP);
            }

            var json = new javascriptSerializer() {MaxJsonLength = int.MaxValue};
            string Encode = json.Serialize(people);

            List> DogDecode = json.Deserialize>>(Encode);

            List> CatDecode = json.Deserialize>>(Encode);
        }
    }

    public class Person
    {
        public int ID { get; set;}
        public string Name { get; set; }
        public int Age { get; set; }
        public DateTime BirthDay { get; set; }
        public List Content { get; set; }
    }

    public class DogInfo
    {
        public string Name { get; set; }
        public string Owner { get; set; }
    }

    public class CatInfo
    {
        public string Name { get; set; }
        public string Owner { get; set; }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         917  2013-07-18 14:10  Demo_Json\Demo_Json.sln
     文件       53760  2013-08-29 11:11  Demo_Json\Demo_Json.v11.suo
     目录           0  2013-08-29 15:35  Demo_Json\Demo_Json\
     文件         167  2013-07-18 14:28  Demo_Json\Demo_Json\App.config
     目录           0  2013-08-29 15:35  Demo_Json\Demo_Json\bin\
     目录           0  2013-08-29 15:35  Demo_Json\Demo_Json\bin\Debug\
     文件       11264  2013-08-29 10:13  Demo_Json\Demo_Json\bin\Debug\Demo_Json.exe
     文件         167  2013-07-18 14:28  Demo_Json\Demo_Json\bin\Debug\Demo_Json.exe.config
     文件       30208  2013-08-29 10:13  Demo_Json\Demo_Json\bin\Debug\Demo_Json.pdb
     文件       22472  2013-08-29 10:14  Demo_Json\Demo_Json\bin\Debug\Demo_Json.vshost.exe
     文件         167  2013-07-18 14:28  Demo_Json\Demo_Json\bin\Debug\Demo_Json.vshost.exe.config
     文件         490  2012-06-06 02:06  Demo_Json\Demo_Json\bin\Debug\Demo_Json.vshost.exe.manifest
     文件        4375  2013-07-18 15:31  Demo_Json\Demo_Json\Demo_Json.csproj
     文件        2842  2013-08-29 10:00  Demo_Json\Demo_Json\Form1.cs
     文件        2823  2013-07-18 16:03  Demo_Json\Demo_Json\Form1.Designer.cs
     文件        5817  2013-07-18 16:04  Demo_Json\Demo_Json\Form1.resx
     目录           0  2013-08-29 15:35  Demo_Json\Demo_Json\obj\
     目录           0  2013-08-29 15:35  Demo_Json\Demo_Json\obj\Debug\
     文件        1632  2013-08-29 10:14  Demo_Json\Demo_Json\obj\Debug\Demo_Json.csproj.FileListAbsolute.txt
     文件         975  2013-08-29 09:51  Demo_Json\Demo_Json\obj\Debug\Demo_Json.csproj.GenerateResource.Cache
     文件        2992  2013-08-29 09:51  Demo_Json\Demo_Json\obj\Debug\Demo_Json.csprojResolveAssemblyReference.cache
     文件       11264  2013-08-29 10:13  Demo_Json\Demo_Json\obj\Debug\Demo_Json.exe
     文件         180  2013-08-29 09:51  Demo_Json\Demo_Json\obj\Debug\Demo_Json.Form1.resources
     文件       30208  2013-08-29 10:13  Demo_Json\Demo_Json\obj\Debug\Demo_Json.pdb
     文件         180  2013-08-29 09:51  Demo_Json\Demo_Json\obj\Debug\Demo_Json.Properties.Resources.resources
     文件        1686  2013-07-18 14:54  Demo_Json\Demo_Json\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        7516  2013-08-29 10:12  Demo_Json\Demo_Json\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件           0  2013-07-18 14:10  Demo_Json\Demo_Json\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2013-07-18 14:10  Demo_Json\Demo_Json\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2013-07-18 14:10  Demo_Json\Demo_Json\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
     目录           0  2013-08-29 15:35  Demo_Json\Demo_Json\obj\Debug\TempPE\
............此处省略9个文件信息

评论

共有 条评论