一. 引用示例
static void Main(string[] args)
{
string FileFullName = "testlog.json";
string[] PropertyNames = new string[4] { "General information", "Serial number" , "Production date" , "Year" };
string Value = "2025";
/*
JObject jo_CCC = new JObject(new JProperty(PropertyNames[PropertyNames.Length-1], Value));
JObject jo_CC = new JObject(new JProperty(PropertyNames[PropertyNames.Length - 2], jo_CCC));
JObject jo_C = new JObject(new JProperty(PropertyNames[PropertyNames.Length - 3], jo_CC));
JObject jo = new JObject( new JProperty(PropertyNames[PropertyNames.Length - 4], jo_C));
JRW_Helper.JToFile(jo, FileFullName); //只能完全覆盖,不能部分修改(相当于完全新建)
*/
List<string[]> NodeNamesLst = JRW_Helper.JGetNodeNamesLst(JRW_Helper.FileToJ(FileFullName));
int n = NodeNamesLst.Max(x => x.Length);
Console.WriteLine("n:" + n);//当n>PropertyNames.Length,则有可能将要修改的数据正好被缩短,可以加如下再判断
bool EventOcurr = false;
if (n> PropertyNames.Length)
{
foreach(string[] Ss in NodeNamesLst)
{
if(Ss.Length > PropertyNames.Length)
{
for(int i=0;i<Ss.Length;i++)
{
if (Ss[i] != PropertyNames[i]) continue;
EventOcurr = true;break;
}
}
if (EventOcurr) break;
}
}
if (EventOcurr) Console.WriteLine("The Event of Note Number be shorted Ocurred");
JRW_Helper.JWriteValue(FileFullName, PropertyNames, Value); //部分修改,不存在则部分新建,有可能节点数被缩短
string value1 = JRW_Helper.JReadValue(FileFullName, PropertyNames);
Console.WriteLine("value1:" + value1);
Console.ReadKey();
}
二. 一中的JRW_Helper 是自己封装的模块,完整版下载 路径见GitHub:
三. 二中有2个还不是很完善的地方,比如:
1. 这两个函数目前只封装到支持最大4个节点,5个节点以上需要自己动手添加,各位有没有 一劳永逸的封装方法,不管多少个节点都能适用的?
2. JGetNodeNameLst这个函数存在如下缺陷,各位有没有更好的方法呢。
四. 三中缺陷问题都不大,基本的适用肯定是不受影响。如有其它不足之处也欢迎各位欢迎批评指正。