string jsonfile = @"E:\history.json";//JSON文件路径 using (System.IO.FileStream file = new FileStream(jsonfile, FileMode.Open, FileAccess.ReadWrite)) { var buffer = new byte[file.Length];//获取用字节表示的流长度 file.Read(buffer, 0, buffer.Length);//0 字节 1 偏移量 2读取的字节长度 var jsonTxt = Encoding.UTF8.GetString(buffer); JObject o = JObject.Parse(jsonTxt);//获取json对象 JToken jt = o["Chapter"];//获取Json中的指定字符 for (int i = 0; i < jt.Count(); i++)//特殊需求 不做理会 { for (int j = 0; j < jt[i]["article"].Count(); j++) { var arttitle = Convert.ToString(jt[i]["article"][j]["arttitle"]); int num = arttitle.LastIndexOf(" "); if (num != -1) { arttitle = arttitle.Substring(num + 1); } jt[i]["article"][j]["path"] = GetHtmlPath(arttitle, (i + 1) + "-" + (j + 1)); } } var result = o.ToString(); var newBuffer = Encoding.UTF8.GetBytes(result); var index = file.Seek(0, SeekOrigin.Begin); file.Write(newBuffer, 0, newBuffer.Count()); }
示例Json
原文地址:https://www.cnblogs.com/fangyyy/p/10775663.html
时间: 2024-10-12 09:14:18