StringBuilder[] 作为数组如何使用

在工作中突然要用到这个就记录下来。

不知为何我这里的StringBuilder[] 数组必须要指明几个(les)才给用,否则就会报错。

   int les = 5;

   StringBuilder[] sb_bj = new StringBuilder[les];

   for (int j = 0; j < ds_JKName.Tables[0].Rows.Count; j++)
   {

      string a = "{ name: '" + ds_JKName.Tables[0].Rows[j]["监控点名称"] + "',data:[";

      sb_bj[j] = new StringBuilder().Append(a); //创建Json开头   注意这里赋值一定要先new StringBuilder() 然后再点.Append(a) 添加值,否则会报错。

    }

-------------------------------------------------------------------------------------------



 

     //根据监控点ID获取监控点名称(名称)
            DataSet ds_JKName = _bll.GetJianKongDianNameListByKongDianID(ids);
            int les = ds_JKName.Tables[0].Rows.Count;
            StringBuilder[] sb_bj = new StringBuilder[les];

            //获取监控名称用于显示折线名称(过程)
            DataSet ds_jiankongdianName = _bll.GetJianKongDianLists(ids, beginDate, endDate, dNum);
                    
            //构建Json格式
            for (int j = 0; j < ds_JKName.Tables[0].Rows.Count; j++)
            {
                if (j == 0)
                {
                    string a = "{ name: '" + ds_JKName.Tables[0].Rows[j]["监控点名称"] + "',data:[";

                    sb_bj[j] = new StringBuilder().Append(a); //创建Json开头
                    

                }
                else
                {
                    sb_bj[j] = new StringBuilder().Append(",{ name: '" + ds_JKName.Tables[0].Rows[j]["监控点名称"] + "',data:[");  //创建Json开头
                }


                DateTime beginDay = DateTime.MinValue;

                for (int i = 0; i < ds_jiankongdianName.Tables[0].Rows.Count; i++)
                {
                    string b = ds_JKName.Tables[0].Rows[j]["监控点ID"].ToString();
                    string c = ds_jiankongdianName.Tables[0].Rows[i]["监控点ID"].ToString();

                    if (b.Equals(c))
                    {
                        beginDay = Convert.ToDateTime(ds_jiankongdianName.Tables[0].Rows[i]["数据时间"]);
                        sb_bj[j].Append("[Date.UTC(" + beginDay.Year + ", " + (beginDay.Month - 1) + ", " + beginDay.Day + ", " + beginDay.Hour + ", " + beginDay.Minute + ")," + ds_jiankongdianName.Tables[0].Rows[i]["avgValue"].ToString() + "],");
                    }
                }

               
                 sb_bj[j].Append("]}");  //创建Json结尾

                 //sb_bj[j].Remove(sb_bj[j].Length -1 ,1);

                 seriesData.Append(sb_bj[j]);

 
            }

 

你可能感兴趣的:(StringBuilder[] 作为数组如何使用)