C# json反序列化 对象中嵌套数组 (转载)

 

看图:

这里可以看到是二层嵌套!!使用C#如何实现??

思路:使用list集合实现 → 建立类 → list集合 → 微软的   Newtonsoft.Json  (一款.NET中开源的Json序列化和反序列化)

sonXMText类

复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonDemo
{
    public class sonXMText
    {
        public string xmleixing { get; set; }

        public string count { get; set; }

        public string xmtype { get; set; }

        public string url { get; set; }

        public string progress { get; set; }

        public string WaitCount { get; set; }
    }
}
复制代码
复制代码

TestInfo类

复制代码
复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonDemo
{
    public class TestInfo
    {
        public string XMText { get; set; }

        public string XMTYPE { get; set; }

        public string Count { get; set; }

        List<sonXMText> sonxmtext = new List<sonXMText>();
        public List<sonXMText> sonXMText
        {
            get { return sonxmtext; }
            set { sonxmtext = value; }
        }
    }
}
复制代码
复制代码
复制代码
复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JsonDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            List<TestInfo> listTestinfo = new List<TestInfo>();
            TestInfo testinfo = new TestInfo();
            testinfo.XMText = "综合交通";
            testinfo.XMTYPE = "01";
            testinfo.Count = "108";

            List<sonXMText> listSonText = new List<sonXMText>();
            sonXMText sontext1 = new sonXMText();
            sontext1.xmleixing = "市重点交通";
            sontext1.xmtype = "32";
            sontext1.count = "20";
            sontext1.url = "../ProjectManage/Pagesilding/ZongHeJiaoTong/Zonghetra/Zonghetra_List.aspx?xmtype=0101";
            sontext1.progress = "52.61";
            sontext1.WaitCount = "27";

            sonXMText sontext2 = new sonXMText();
            sontext1.xmleixing = "支路网建设";
            sontext1.xmtype = "32";
            sontext1.count = "20";
            sontext1.url = "../ProjectManage/Pagesilding/ZongHeJiaoTong/Zonghetra/Zonghetra_List.aspx?xmtype=0101";
            sontext1.progress = "52.61";
            sontext1.WaitCount = "27";

            listSonText.Add(sontext1);
            listSonText.Add(sontext2);
            testinfo.sonXMText = listSonText;

            listTestinfo.Add(testinfo);

            string aa = Newtonsoft.Json.JsonConvert.SerializeObject(listTestinfo);
            File.AppendAllText(@"C:\mymiao.txt", aa, Encoding.UTF8);

        }
    }
}
复制代码
复制代码

 

 

来源:https://www.cnblogs.com/panmy/p/5924324.html

 

分类: 转载
标签: C#基础
 
posted @   hao_1234_1234  阅读(6152)  评论(3编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
点击右上角即可分享
微信分享提示