如何读取JSON文件

本文关键字:JSON 文件 读取 何读取 | 更新日期: 2023-09-27 18:16:43

我有一个文件与以下格式化Json

{
  "Id": 0,
  "MsgId": 125,
  "ExceptionDetails": "whatever2"
}

{
  "Id": 1,
  "MsgId": 135,
  "ExceptionDetails": "whatever2"
}

在没有括号的文件中就是这样。

我需要解析这个文本文件并获得这些键的值,例如,在这个例子中,我需要获得0和1

感谢

,这就是它如何被写入文件,所以我可能没有以正确的JSON格式写入文件

 string json = JsonConvert.SerializeObject(logs, Formatting.Indented);
 using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:'development'commonArea'WriteLines.txt", true))
                {
                    file.WriteLine(json);
                }

如何读取JSON文件

这是你的原始文件吗?如果是,则无效json.

在这种情况下,你可以做的是使用字符串分割或regex-fu将文件分割成单独的json对象,然后使用内置的JavaScriptSerializer或Newtonsoft.Json对它们进行解析。

var x = [{
  "Id": 0,
  "MsgId": 125,
  "ExceptionDetails": "whatever2"
},

{
  "Id": 1,
  "MsgId": 135,
  "ExceptionDetails": "whatever2"
}]
x[0].Id // 0
x[1].Id // 1