C # achieve the objects created Json file is nested arrays

FIG effect nested array Json file:

First, the idea of analysis :

① layer 2 is nested observed

② use the List collection implementation ---> create a class ---> List collection ---> Newtonsoft.json.dll

Second, the implementation steps

2.1, the second layer creating nested base class: MapInfo.cs

namespace JsonDemo
{
    class MapInfo
    {
        public string mapName { get; set; }
        public string count { get; set; }
        public string type { get; set; }
        public string url { get; set; }
        public string progress { get; set; }
        public string waitCount { get; set; }

    }//Class_end
}

 2.2, the first layer to create nested classes: TestInfo.cs

using System.Collections.Generic;

namespace JsonDemo
{
    class TestInfo
    {
        public string XMText { get; set; }
        public string XMTYPE { get; set; }
        public string XMCount { get; set; }
        List<MapInfo> mapInfo = new List<MapInfo>();
        public List<MapInfo> mapInfos
        {
            get { return mapInfo; }
            set { mapInfo = value; }
        }

    }//Class_end
}

2.3, Json create nested arrays to achieve the object file

using JsonDemo;
using Kernal;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace XmlHelper
{
    class Program
    {
        static void Main(string[] args)
        {
            List<TestInfo> testInfos = new List<TestInfo>();
            TestInfo testInfo1 = new TestInfo();
            testInfo1.XMText = "综合交通1";
            testInfo1.XMTYPE = "01";
            testInfo1.XMCount = "108";

            TestInfo testInfo2 = new TestInfo();
            testInfo2.XMText = "综合交通2";
            testInfo2.XMTYPE = "02";
            testInfo2.XMCount = "109";

            //信息1的子内容
            List<MapInfo> mapInfos1 = new List<MapInfo>();
            MapInfo mapInfo1_1 = new MapInfo();
            mapInfo1_1.mapName = "市重点交通";
            mapInfo1_1.type = "32";
            mapInfo1_1.count = "20";
            mapInfo1_1.url = "../ProjectManage/Pagessilding/Zhonghejiaotong/Zhonghetra_list.aspx?type=0101";
            mapInfo1_1.progress = "52.61";
            mapInfo1_1.waitCount = "27";

            MapInfo mapInfo1_2 = new MapInfo();
            mapInfo1_2.mapName = "支路网建设";
            mapInfo1_2.type = "32";
            mapInfo1_2.count = "20";
            mapInfo1_2.url = "../ProjectManage/Pagessilding/Zhonghejiaotong/Zhonghetra_list.aspx?type=0111";
            mapInfo1_2.progress = "52.61";
            mapInfo1_2.waitCount = "27";

            //信息2的子内容
            List<MapInfo> mapInfos2 = new List<MapInfo>();
            MapInfo mapInfo2_1 = new MapInfo();
            mapInfo2_1.mapName = "市重点交通2";
            mapInfo2_1.type = "32";
            mapInfo2_1.count = "20";
            mapInfo2_1.url = "../ProjectManage/Pagessilding/Zhonghejiaotong/Zhonghetra_list.aspx?type=1101";
            mapInfo2_1.progress = "52.61";
            mapInfo2_1.waitCount = "30";

            MapInfo mapInfo2_2 = new MapInfo();
            mapInfo2_2.mapName = "支路网建设2";
            mapInfo2_2.type = "32";
            mapInfo2_2.count = "20";
            mapInfo2_2.url = "../ProjectManage/Pagessilding/Zhonghejiaotong/Zhonghetra_list.aspx?type=1111";
            mapInfo2_2.progress = "52.61";
            mapInfo2_2.waitCount = "30";

            //信息1内容
            mapInfos1.Add(mapInfo1_1);
            mapInfos1.Add(mapInfo1_2);
            testInfo1.mapInfos = mapInfos1;

            //信息2内容
            mapInfos2.Add(mapInfo2_1);
            mapInfos2.Add(mapInfo2_2);
            testInfo2.mapInfos = mapInfos2;

            //整合信息1、信息2内容
            testInfos.Add(testInfo1);
            testInfos.Add(testInfo2);


            string str = Newtonsoft.Json.JsonConvert.SerializeObject(testInfos);
            //保存文件
            bool success = SaveFile(@"e:\Zhonghejiaotong.json", str);
            Console.WriteLine("保存情况="+success);
            //File.AppendAllText(@"e:\Zhonghejiaotong.json",str,Encoding.UTF8);

            Console.ReadLine();

        }//Class_end


        //保存文件
        public static bool SaveFile(string path,string contents)
        {
            bool success = false;
            //追加文件
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, FileShare.ReadWrite))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    //开始写入
                    sw.WriteLine(contents);
                    success = true;
                }
            }
            return success;
        }

    }
}

 Note: This content from: C # nested array of objects json

 

Guess you like

Origin blog.csdn.net/xiaochenXIHUA/article/details/90637944