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

 

 

转载于:https://www.cnblogs.com/hao-1234-1234/p/8667373.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
{"error":0,"status":"success","date":"2015-12-21","results":[{"currentCity":"上海","pm25":"205","index":[{"title":"穿衣","zs":"较冷","tipt":"穿衣指数","des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,但丝毫不会影响您出行的心情。温度适宜又有微风相伴,适宜旅游。"},{"title":"感冒","zs":"较易发","tipt":"感冒指数","des":"天气较凉,较易发生感冒,请适当增加衣服。体质较弱的朋友尤其应该注意防护。"},{"title":"运动","zs":"较适宜","tipt":"运动指数","des":"天气较好,无雨水困扰,较适宜进行各种运动,但因气温较低,在户外运动请注意增减衣物。"},{"title":"紫外线强度","zs":"最弱","tipt":"紫外线强度指数","des":"属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"}],"weather_data":[{"date":"周一 12月21日 (实时:6℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/yin.png","weather":"多云转阴","wind":"东北风微风","temperature":"11 ~ 8℃"},{"date":"周二","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/zhongyu.png","weather":"小雨转雨","wind":"东风微风","temperature":"13 ~ 10℃"},{"date":"周三","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"北风微风","temperature":"12 ~ 10℃"},{"date":"周四","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/xiaoyu.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/xiaoyu.png","weather":"小雨","wind":"北风微风","temperature":"7 ~ 4℃"}]}]}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值