我需要你的帮助.我是C#的新手,所以我很感激任何帮助!
所以我创建了一个解析json文件并将其放入对象的程序.
我的程序看起来像这样:
所以我创建了一个解析json文件并将其放入对象的程序.
我的程序看起来像这样:
var path = (@"C:\sim\input7.json"); using (StreamReader reader = File.OpenText(path)) { JObject SimData = JObject.Parse(reader.ReadLine()); Console.WriteLine("Object ID: " + SimData["_id"]); Console.WriteLine("ID: " + SimData["id"]); Console.WriteLine("Day: " + SimData["day"]); Console.WriteLine("Value: " + SimData["value"]);
首先,我在我的json文件中只使用一行,它工作正常.
现在我有多行,例如:
{ "_id" : "_id1","id" : "100","day" : "5","value" : "90.38","time" : "000000" } { "_id" : "_id2","id" : 100,"day" : 5,"value" : 89.79000000000001,"time" : "000100" }
我的问题是,如果我想要Console.WriteLine(),该怎么办?每个值例如!
我尝试了一些东西,但一个不能解决这个问题.我有相同的输入文件.我现在试过这个:
using (StreamReader reader = new StreamReader(path)) { string line; while ((line = reader.ReadLine()) != null) { list.Add(line); JObject SD = JObject.Parse(line); Console.WriteLine(SD["day"]); } }
想要为每一行只写出“价值”!
解决方法
分别解析每个JSon文件:
foreach(DirectoryInfo directoryInfo in new DirectoryInfo(@"C:\sim\").getDirectories()); { using (StreamReader reader = File.OpenText(directoryInfo.FullName)) { JObject SimData = JObject.Parse(reader.ReadLine()); Console.WriteLine("Object ID: " + SimData["_id"]); Console.WriteLine("ID: " + SimData["id"]); Console.WriteLine("Day: " + SimData["day"]); Console.WriteLine("Value: " + SimData["value"]); } }