c#读取txt文件

using LitJson;

 private void btnRead_Click(object sender, EventArgs e)
        {
            string path = @"D:/test.json";//读取文件txt  
            using (FileStream fs = new FileStream(path, FileMode.Open))
            {
                 using (StreamReader sr = new StreamReader(fs,UnicodeEncoding.GetEncoding("GB2312")))
                 {
                      while (!sr.EndOfStream) 
                      { 
                           string sLine = sr.ReadToEnd(); 
                           if(sLine.Length>0)
                           {
                               JsonData jd = JsonMapper.ToObject(sLine);
                               this.textBox1.Text = (String)jd["name"];


                               //long id = (long)jd["id"];
                               JsonData jdItems = jd["items"];
                               //int itemCnt = jdItems.Count;
                               //// 数组 items 中项的数量
                               foreach (JsonData item in jdItems)
                               // 遍历数组 items           
                               {
                                   int itemID = (int)item["itemid"];
                                   String itemName = (String)item["itemname"];
                                   this.textBox1.Text += itemID.ToString() + " " + itemName.ToString();
                               }

                               ////*** 将JsonData转换为JSON字符串 ***************************         

                               //String str2 = jd.ToJson();
                           }
                      } 
                 }
            }
        }

posted on 2013-08-04 19:41  vedusoft  阅读(371)  评论(0编辑  收藏  举报