测试按钮代码如下
string jsonfile = Application.StartupPath + @"\config.json";//JSON文件路径
//json格式字符串转换为实体类,大括号{}表示对象,[]数组表示列表。
try
{
String input = "";
//读json方法1
string Jsonsting = File.ReadAllText(jsonfile,Encoding.Default);//加编码,不然乱码
input = Jsonsting;
//读取json方法2
//using (StreamReader sr = File.OpenText(jsonfile))
//{
// input = sr.ReadToEnd();//json内容
// sr.Close();
//}
tmp_rtxlog(string.Format("修改前原始数据【{0}】",input));//完整文本
rtbx_log.Visible = true;//调试显示日志
rtbx_log.Dock = DockStyle.Fill;
//测试读取指定值方法
//Root root = JsonConvert.DeserializeObject(input);//根目录
//LedControlItem leds = JsonConvert.DeserializeObject(input);//控制器
//SprogItem sprog = JsonConvert.DeserializeObject(input);//静态节目列表
//ProgListItem plist = JsonConvert.DeserializeObject(input);//静态节目单列表
//DprogItem dprog = JsonConvert.DeserializeObject(input);//动态节目列表
//tmp_rtxlog("屏总数量:"+root.LedControl.Count.ToString());
//tmp_rtxlog("屏1的名称:" + root.LedControl[0].screenName);
//tmp_rtxlog("屏2的名称:" + root.LedControl[1].screenName);
//tmp_rtxlog("屏1静态节目1的名称:" + root.LedControl[0].sprog[0].progName);
//tmp_rtxlog("屏2动态节目2的名称:" + root.LedControl[1].dprog[1].pName);
//*******************以下重新组合后写保存************************
List pList = new List();
ProgListItem pli = new ProgListItem
{//直接写在这里面,不需要加pli,单独赋值时需要加pli
pName="售票屏A静节目1节目单1新000",
//其他值省略...字符串型不赋值会默认null,数字的会默认0下同
ledConts= "售票屏A静节目1欢迎您1新000"
};
ProgListItem pli1 = new ProgListItem
{//直接写在这里面,不需要加pli,单独赋值时需要加pli
pName = "售票屏A静节目1节目单2新111",
//其他值省略...字符串型不赋值会默认null,数字的会默认0下同
ledConts = "售票屏A静节目1欢迎您2新111"
};
pList.Add(pli);
pList.Add(pli1);//静态节目1里的2个节目单
List lst_spi = new List();
SprogItem spi = new SprogItem
{
progName= "售票屏A静节目1新新",
//其他值省略...字符串型不赋值会默认null,数字的会默认0下同
progList=pList
};
SprogItem spi1 = new SprogItem
{
progName = "售票屏A静节目1新新",
//其他值省略...字符串型不赋值会默认null,数字的会默认0下同
progList = pList
};
lst_spi.Add(spi);
lst_spi.Add(spi1);
List lst_dpi = new List();
DprogItem _dpi = new DprogItem
{
pName="售票屏A动态1-new",
//其他值省略...字符串型不赋值会默认null,数字的会默认0下同
ledConts = "新内容1"
};
DprogItem _dpi1 = new DprogItem
{
pName = "售票屏A动态2-new",
//其他值省略...字符串型不赋值会默认null,数字的会默认0下同
ledConts = "新内容2"
};
lst_dpi.Add(_dpi);
lst_dpi.Add(_dpi1);
List lst_lci = new List();
LedControlItem _lci=new LedControlItem{
screenName="屏AAA",
screenNo = "1",
//其他值省略...字符串型不赋值会默认null,数字的会默认0下同
sprog = lst_spi,
dprog=lst_dpi
};
LedControlItem _lci1 = new LedControlItem
{
screenName = "屏BBB",
screenNo = "2",
//其他值省略...字符串型不赋值会默认null,数字的会默认0下同
sprog = lst_spi,//可以新的(这里仅演示,重复调用了)
dprog = lst_dpi
};
lst_lci.Add(_lci);
lst_lci.Add(_lci1);
List lst_root = new List();
Root _root = new Root
{
LedControl = lst_lci
};
lst_root.Add(_root);//因为此不是list了
//对象转字符串后保存新的字符串到对象
string new_json = JsonConvert.SerializeObject(lst_root, Formatting.Indented);//最外层的对象反实例化后写保存
//Root root = JsonConvert.DeserializeObject(new_json);//字符串转对象
//tmp_rtxlog(string.Format( "新内容【{0}】",new_json));
////去除开始的"["和结尾的"]"
string New_json = new_json.Substring(1,new_json.Length- 2);//去掉首-尾的[]
////去除字符中间的空隙
//New_json = New_json.Replace("\r\n", "");
//New_json = New_json.Replace("\t", "");
tmp_rtxlog(string.Format("修改后新数据【{0}】", New_json));
File.WriteAllText(jsonfile, New_json, Encoding.Default);//路径,内容,编码
//读取的时候要 字符串转对象
Root root = JsonConvert.DeserializeObject(New_json);//根目录
tmp_rtxlog("读取屏1的新名称:" + root.LedControl[0].screenName);
}
catch(Exception ex) {
tmp_rtxlog(ex.Message.ToString());
}
上面代码中 tmp_rtxlog是一个回显的richtextbox文本框的方法
config.json
{
"LedControl": [
{
"screenName": "屏AAA",
"screenNo": "1",
"screenPort": 0,
"screenWidth": 0,
"screenHeight": 0,
"screenFaces": 0,
"sortIndex": 0,
"sprog": [
{
"progName": "售票屏A静节目1新新",
"displayType": 0,
"displayTimesSeconds": 0,
"allDay": 0,
"allTime": 0,
"allWeek": 0,
"beginYear": 0,
"beginMonth": 0,
"beginDay": 0,
"beginHour": 0,
"beginMinute": 0,
"beginSecond": 0,
"endYear": 0,
"endMonth": 0,
"endDay": 0,
"endHour": 0,
"endMinute": 0,
"endSecond": 0,
"isPlayAlltime": 0,
"IsValidAlways": 0,
"progList": [
{
"pName": "售票屏A静节目1节目单1新000",
"progNo": 0,
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "售票屏A静节目1欢迎您1新000"
},
{
"pName": "售票屏A静节目1节目单2新111",
"progNo": 0,
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "售票屏A静节目1欢迎您2新111"
}
]
},
{
"progName": "售票屏A静节目1新新",
"displayType": 0,
"displayTimesSeconds": 0,
"allDay": 0,
"allTime": 0,
"allWeek": 0,
"beginYear": 0,
"beginMonth": 0,
"beginDay": 0,
"beginHour": 0,
"beginMinute": 0,
"beginSecond": 0,
"endYear": 0,
"endMonth": 0,
"endDay": 0,
"endHour": 0,
"endMinute": 0,
"endSecond": 0,
"isPlayAlltime": 0,
"IsValidAlways": 0,
"progList": [
{
"pName": "售票屏A静节目1节目单1新000",
"progNo": 0,
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "售票屏A静节目1欢迎您1新000"
},
{
"pName": "售票屏A静节目1节目单2新111",
"progNo": 0,
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "售票屏A静节目1欢迎您2新111"
}
]
}
],
"dprog": [
{
"pName": "售票屏A动态1-new",
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "新内容1"
},
{
"pName": "售票屏A动态2-new",
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "新内容2"
}
]
},
{
"screenName": "屏BBB",
"screenNo": "2",
"screenPort": 0,
"screenWidth": 0,
"screenHeight": 0,
"screenFaces": 0,
"sortIndex": 0,
"sprog": [
{
"progName": "售票屏A静节目1新新",
"displayType": 0,
"displayTimesSeconds": 0,
"allDay": 0,
"allTime": 0,
"allWeek": 0,
"beginYear": 0,
"beginMonth": 0,
"beginDay": 0,
"beginHour": 0,
"beginMinute": 0,
"beginSecond": 0,
"endYear": 0,
"endMonth": 0,
"endDay": 0,
"endHour": 0,
"endMinute": 0,
"endSecond": 0,
"isPlayAlltime": 0,
"IsValidAlways": 0,
"progList": [
{
"pName": "售票屏A静节目1节目单1新000",
"progNo": 0,
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "售票屏A静节目1欢迎您1新000"
},
{
"pName": "售票屏A静节目1节目单2新111",
"progNo": 0,
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "售票屏A静节目1欢迎您2新111"
}
]
},
{
"progName": "售票屏A静节目1新新",
"displayType": 0,
"displayTimesSeconds": 0,
"allDay": 0,
"allTime": 0,
"allWeek": 0,
"beginYear": 0,
"beginMonth": 0,
"beginDay": 0,
"beginHour": 0,
"beginMinute": 0,
"beginSecond": 0,
"endYear": 0,
"endMonth": 0,
"endDay": 0,
"endHour": 0,
"endMinute": 0,
"endSecond": 0,
"isPlayAlltime": 0,
"IsValidAlways": 0,
"progList": [
{
"pName": "售票屏A静节目1节目单1新000",
"progNo": 0,
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "售票屏A静节目1欢迎您1新000"
},
{
"pName": "售票屏A静节目1节目单2新111",
"progNo": 0,
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "售票屏A静节目1欢迎您2新111"
}
]
}
],
"dprog": [
{
"pName": "售票屏A动态1-new",
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "新内容1"
},
{
"pName": "售票屏A动态2-new",
"areaX": 0,
"areaY": 0,
"areaW": 0,
"areaH": 0,
"rowHeight": 0,
"sigleLine": 0,
"runMode": 0,
"runMode2OverTimeDel": 0,
"displayMode": 0,
"runSpeed": 0,
"staySecond": 0,
"ledConts": "新内容2"
}
]
}
]
}
json类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//json生成c#类工具http://www.bejson.com/convert/json2csharp/
namespace LED6K3
{///共计5个类(根类Root)
public class ProgListItem
{
///
/// 【节目单名称】
///
public string pName { get; set; }
///
/// 【节目单与类DprogItem一样:动态节目和静态节目共用属性,就名称字段不一致】 - 编号(数字)
///
public int progNo { get; set; }
///
/// 节目单起始坐标X(数字)
///
public int areaX { get; set; }
///
/// 节目单起始坐标Y(数字)
///
public int areaY { get; set; }
///
/// 节目单区域宽度(数字)
///
public int areaW { get; set; }
///
/// 节目单区域高度(数字)
///
public int areaH { get; set; }
///
/// 节目单行高(数字)
///
public int rowHeight { get; set; }
///
/// 节目单单行显示1,多行显示2(数字)
///
public int sigleLine { get; set; }
///
/// 节目单运行模式:0循环模式,1循环一次后静止,2超时删除
///
public int runMode { get; set; }
///
/// 节目单运行模式为2时,超时删除的时间秒
///
public int runMode2OverTimeDel { get; set; }
///
/// 节目单显示特技:0静止,1快打,2向左,3向右,4向上,5向下
///
public int displayMode { get; set; }
///
/// 运行速度;其他模式自由设置(最大24)
///
public int runSpeed { get; set; }
///
/// 停留时间(0.5的倍数*速度)秒,快打时默认2(farstest);
///
public int staySecond { get; set; }
///
/// 【静态节目单】屏幕显示的内容文本(注意多色时转移字符的使用)
///
public string ledConts { get; set; }
}
public class SprogItem
{
///
/// 【静态节目】 - 节目名称(静态节目的独有字段)
///
public string progName { get; set; }
///
/// 静态节目-显示方式:0顺序播放,1定时播放
///
public int displayType { get; set; }
///
/// 静态节目-显示方式:0顺序播放时的次数或1定时播放秒数
///
public int displayTimesSeconds { get; set; }
///
/// 静态节目-全天有效1,其他为0
///
public int allDay { get; set; }
///
/// 静态节目-全时段0,其他为1
///
public int allTime { get; set; }
///
/// 静态节目-全周有效1(其他情况是选定的周的byte数据)需要计算
///
public int allWeek { get; set; }
///
/// 开始年
///
public int beginYear { get; set; }
///
/// 开始月
///
public int beginMonth { get; set; }
///
/// 开始日
///
public int beginDay { get; set; }
///
/// 开始时
///
public int beginHour { get; set; }
///
/// 开始分
///
public int beginMinute { get; set; }
///
/// 开始秒
///
public int beginSecond { get; set; }
///
/// 截止年
///
public int endYear { get; set; }
///
/// 截止月
///
public int endMonth { get; set; }
///
/// 截止日
///
public int endDay { get; set; }
///
/// 截止时
///
public int endHour { get; set; }
///
/// 截止分
///
public int endMinute { get; set; }
///
/// 截止秒
///
public int endSecond { get; set; }
///
/// 全时段有效1,其他0??
///
public int isPlayAlltime { get; set; }
///
/// 一直有效1,其他0
///
public int IsValidAlways { get; set; }
///
/// 【静态节目】列表
///
public List progList { get; set; }
}
public class DprogItem
{
///
/// 【动态节目-与静态节目同级别;可以与ProgListItem合并,仅1字段不同progName和pName】名称
///
public string pName { get; set; }
///
/// 动态节目-区域起始X坐标
///
public int areaX { get; set; }
///
/// 动态节目-区域起始Y坐标
///
public int areaY { get; set; }
///
/// 动态节目-区域宽度
///
public int areaW { get; set; }
///
/// 动态节目-区域高度
///
public int areaH { get; set; }
///
/// 动态节目-行高
///
public int rowHeight { get; set; }
///
/// 动态节目-单行显示1,多行显示2(数字)
///
public int sigleLine { get; set; }
///
/// 动态节目-运行模式:0循环模式,1循环一次后静止,2超时删除
///
public int runMode { get; set; }
///
/// 动态节目-运行模式为2时,超时删除的时间秒
///
public int runMode2OverTimeDel { get; set; }
///
/// 动态节目-显示特技:0静止,1快打,2向左,3向右,4向上,5向下
///
public int displayMode { get; set; }
///
/// 动态节目-运行速度;其他模式自由设置(最大24)
///
public int runSpeed { get; set; }
///
/// 动态节目-停留时间(0.5的倍数*速度)秒,快打时默认2(farstest)
///
public int staySecond { get; set; }
///
/// 【动态节目】显示内容
///
public string ledConts { get; set; }
}
public class LedControlItem
{
///
/// 【LED屏或控制器类】-屏幕名称(回显便于识别)
///
public string screenName { get; set; }
///
/// 屏编号
///
public string screenNo { get; set; }
///
/// 屏(控制器)端口号
///
public int screenPort { get; set; }
///
/// 屏宽度
///
public int screenWidth { get; set; }
///
/// 屏高度
///
public int screenHeight { get; set; }
///
/// 屏:单面屏/双面屏(保留使用)
///
public int screenFaces { get; set; }
///
/// 屏索引号(从0开始编号也是排序的依据不能重复)
///
public int sortIndex { get; set; }
///
/// 【静态节目列表】
///
public List sprog { get; set; }
///
/// 【动态节目列表】
///
public List dprog { get; set; }
}
public class Root
{
///
/// 【控制器列表】
///
public List LedControl { get; set; }
}
}
【温馨提示】
第一步:
要引用组件(Newtonsoft.Json.dll)
第二步:
引用类
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;//引用此组件时,框架4.0以上版本支持;注意版本支持的范围
using Newtonsoft.Json.Converters;
json语法校验测试
https://www.sojson.com/simple_json.html