
C#实现对象为Json嵌套数组文件的创建
������������������������������ ������������������������������ ���������������������Newtonsoft.json���������C#������������������JSON��������������������������������������������� 2.1 ������������������
2.2 ���������������������������
发布日期:2021-05-10 09:24:53
浏览次数:12
分类:精选文章
本文共 4747 字,大约阅读时间需要 15 分钟。
Json������������������������������
������������������������
���������������������������������������������������������������������JSON������������������������������������C#���Newtonsoft.json���������������������������������������������������������������������������
TestInfo
���������������������������������List<MapInfo>
������������������������������������������MapInfo
������������������������������������������������mapName
���count
���type
������������������������������������������
������������������������
2.1 ������������������MapInfo.cs
using System;using System.Collections.Generic;namespace JsonDemo{ public class MapInfo { public string mapName { get; set; } public string type { get; set; } public string count { get; set; } public string url { get; set; } public string progress { get; set; } public string waitCount { get; set; } }}
2.2 ���������������������������TestInfo.cs
using System.Collections.Generic;using Newtonsoft.Json;namespace JsonDemo{ public class TestInfo { public string XMText { get; set; } public string XMTYPE { get; set; } public string XMCount { get; set; } public ListmapInfos { get; set; } public TestInfo() { mapInfos = new List (); } }}
2.3 ���������������������JSON������
������������������������������������������������������JSON���������
using JsonDemo;using Newtonsoft.Json;using System;using System.Collections.Generic;using System.IO;using System.Linq;namespace JsonHelper{ public class Program { public static void Main(string[] args) { ListtestInfos = new List (); // ������������������ 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"; // ��������������������� List mapInfos1 = new List (); 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"; mapInfos1.Add(mapInfo1_1); mapInfos1.Add(mapInfo1_2); testInfo1.mapInfos = mapInfos1; // ��������������������� List mapInfos2 = new List (); 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"; mapInfos2.Add(mapInfo2_1); mapInfos2.Add(mapInfo2_2); testInfo2.mapInfos = mapInfos2; // ������������ testInfos.Add(testInfo1); testInfos.Add(testInfo2); // ������������JSON string jsonContent = Newtonsoft.Json.JsonConvert.SerializeObject(testInfos); // ������������ bool success = SaveFile(@"e:\Zhonghejiaotong.json", jsonContent); Console.WriteLine("���������������" + success); } private static bool SaveFile(string path, string contents) { bool success = false; using (FileStream fs = new FileStream(path, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) { sw.WriteLine(contents); success = true; } } return success; } }}
####Notice of Information
���������������������������������������������������������������
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月09日 07时31分55秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
MySQL错误日志(Error Log)
2021-05-09
oracle使用DBMS_RANDOM包生成随机数据
2021-05-09
C++高精度模板
2021-05-09
联赛模拟测试23 D. 真相 思维题
2021-05-09
SSISDB2:SSIS工程的操作实例
2021-05-09
业务工作流平台设计(八)
2021-05-09
解决:angularjs radio默认选中失效问题
2021-05-09
windows环境下安装zookeeper(仅本地使用)
2021-05-09
缓冲区溢出实例(一)--Windows
2021-05-09
Badboy录制脚本时,提示脚本错误的解决方法
2021-05-09
PHP一句话木马小总结与SQL语句写一句话木马
2021-05-09
关于计数排序
2021-05-09
Python中字符串前添加r ,b, u, f前缀的含义
2021-05-09
Hadoop学习笔记—Yarn
2021-05-09
__format__
2021-05-09
JSONPath小试牛刀之Snack3
2021-05-09
Jenkins - 部署在Tomcat容器里的Jenkins,提示“反向代理设置有误”
2021-05-09
2017年前端框架、类库、工具大比拼
2021-05-09
wxWidgets源码分析(1) - App启动过程
2021-05-09
wxWidgets源码分析(3) - 消息映射表
2021-05-09