Array
(
  [0] => Array
    (
      [musicname] => 天涯
      [singer] => 爱乐团
      [musicurl] => http://mp3.baidu.com/123.mp3
      [lyricurl] => http://mp3.baidu.com/123.lrc
    )  [1] => Array
    (
      [musicname] => 彩云追月
      [singer] => 爱戴
      [musicurl] =>http:// mp3.baidu.com/234.mp3
      [lyricurl] => http://mp3.baidu.com/234.lrc
    )  [2] => Array
    (
      [musicname] => 爱江山更爱美人
      [singer] => 佚名
      [musicurl] => http:// mp3.baidu.com/345.mp3
      [lyricurl] => http:// mp3.baidu.com/345.lrc
  )
  .  .  .)
我取别人网站的一个接口,然后我程序怎么把取到的这段数据转换成dataTable

解决方案 »

  1.   


     DataTable dataTable = new DataTable();
                dataTable.Columns.AddRange(new DataColumn[] {
                new DataColumn("musicname",typeof(System.String)),
                new DataColumn("singer",typeof(System.String)),
                new DataColumn("musicurl",typeof(System.String)),
                new DataColumn("lyricurl",typeof(System.String))
                });            DataRow newRow = dataTable.NewRow();
                newRow["musicname"] = "天涯";
                newRow["singer"] = "爱乐团";
                newRow["musicurl"] = "http://mp3.baidu.com/123.mp3";
                newRow["lyricurl"] = "http://mp3.baidu.com/123.lrc";
                dataTable.Rows.Add(newRow);            newRow = dataTable.NewRow();
                newRow["musicname"] = " 彩云追月";
                newRow["singer"] = "爱戴";
                newRow["musicurl"] = "http:// mp3.baidu.com/234.mp3";
                newRow["lyricurl"] = "http://mp3.baidu.com/234.lrc";
                dataTable.Rows.Add(newRow);            newRow = dataTable.NewRow();
                newRow["musicname"] = " 爱江山更爱美人";
                newRow["singer"] = " 佚名";
                newRow["musicurl"] = "http:// mp3.baidu.com/345.mp3";
                newRow["lyricurl"] = "http://mp3.baidu.com/345.lrc";
                dataTable.Rows.Add(newRow);
    添加行我这里是直接写的,如果你是集合就遍历集合然后添加DataTable中
      

  2.   

    string s = @"Array
    (
      [0] => Array
        (
          [musicname] => 天涯
          [singer] => 爱乐团
          [musicurl] => http://mp3.baidu.com/123.mp3
          [lyricurl] => http://mp3.baidu.com/123.lrc
        )  [1] => Array
        (
          [musicname] => 彩云追月
          [singer] => 爱戴
          [musicurl] =>http://mp3.baidu.com/234.mp3
          [lyricurl] => http://mp3.baidu.com/234.lrc
        )  [2] => Array
        (
          [musicname] => 爱江山更爱美人
          [singer] => 佚名
          [musicurl] => http://mp3.baidu.com/345.mp3
          [lyricurl] => http://mp3.baidu.com/345.lrc
      )";
    MatchCollection matches = Regex.Matches(s, @"(?is)(?<=Array\s*\(\s*.*?)\[\d+\]\s*=>\s*Array\s*\(\s*(?:\[(?<name>\w+)\]\s*=>\s*(?<value>.+?)\r\n\s*)+");
    DataTable dt = new DataTable();
    foreach (Capture capture in matches[0].Groups["name"].Captures)
    dt.Columns.Add(capture.Value, typeof(string));
    foreach (Match match in matches)
    {
    dt.Rows.Add(match.Groups["value"].Captures[0].Value, match.Groups["value"].Captures[1].Value, match.Groups["value"].Captures[2].Value, match.Groups["value"].Captures[3].Value);
    }
    GridView1.DataSource = dt;
    GridView1.DataBind();
      

  3.   

    dalmeeme  正则貌似不正确
      

  4.   

    这个 要先去了解一下如何创建一个datatable 给你找个小例子 参考一下吧。  public DataTable  bind()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("a", typeof(int));
            dt.Columns.Add("b", typeof(int));
            DataRow dr = dt.NewRow();
            dr["a"] = 1;
            dr["b"] = 1;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["a"] =2;
            dr["b"] = 2;
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["a"] = 3;
            dr["b"] = 3;
            dt.Rows.Add(dr);
            return dt;    }