c# txt中Json数据读取

 string json = Jsonstr(fileName);//Jsonstr函数读取json数据的文本txt                            
 JsonDataT result = JsonConvert.DeserializeObject<JsonDataT>(json);
/// <summary>
/// 转换txt中json
/// </summary>
/// <param name="filePath">文件路径</param>
/// <returns></returns>
public string Jsonstr(String filePath)
{
  string strData = "";
  try
  {
      string line;
      // 创建一个 StreamReader 的实例来读取文件 ,using 语句也能关闭 StreamReader,防止中文乱码
      using (System.IO.StreamReader sr = new System.IO.StreamReader(filePath,Encoding.GetEncoding("gb2312")))
      {
          // 从文件读取并显示行,直到文件的末尾
          while ((line = sr.ReadLine()) != null)
          {
              //Console.WriteLine(line);
              strData = line;
          }
      }
  }
  catch (Exception e)
  {
      // 向用户显示出错消息
      Console.WriteLine("The file could not be read:");
      Console.WriteLine(e.Message);
  }
  return strData;
}
posted @ 2020-06-05 14:42  唯爱金生  阅读(1097)  评论(0编辑  收藏  举报