数据表结果Json,我的Json有什么问题



我试图从SQL存储过程到数据表的结果并将其转换为Json,所以以后我可以使用它与Highcharts。由于某些原因,我得到的结果是错误的格式:

我的控制器:

 public ActionResult GetDailyOutputByHour(int machine, string dateFrom, string dateTo, string line = "All")
    {
        Graph g = repository.Graphs.Where(p => p.GraphID == 1).FirstOrDefault();
        string result = repository.GetGraphData(g.StoredProc + " '" + line + "', " + machine + ", '" + dateFrom + "', '" + dateTo + "'");
        return Content(result, "application/json");
    }
存储库:

public string GetGraphData(string sp)
    {
        var connection = ConfigurationManager.ConnectionStrings["EFDbContext"].ConnectionString;
        using (var dataAdapter = new SqlDataAdapter("exec " + sp, connection))
        {
            var dataTable = new DataTable();
            dataAdapter.Fill(dataTable);
            dataAdapter.FillSchema(dataTable, SchemaType.Mapped);
            string json =  JsonConvert.SerializeObject(dataTable, Formatting.Indented);
            return json;
        }
    }

得到的结果:

"[rn  {rn    "Hour": 7,rn    "Input": 4614,rn    "Output": 2952,rn    "InTarget": 3033,rn    "OutTarget": 2533rn  },rn  {rn    "Hour": 8,rn    "Input": 3729,rn    "Output": 2940,rn    "InTarget": 3033,rn    "OutTarget": 2533rn  },rn  {rn    "Hour": 9,rn    "Input": 4158,rn    "Output": 3798,rn    "InTarget": 3033,rn    "OutTarget": 2533rn  },rn  {rn    "Hour": 10,rn    "Input": 5310,rn    "Output": 4023,rn    "InTarget": 3033,rn    "OutTarget": 2533rn  },rn  {rn    "Hour": 11,rn    "Input": 1344,rn    "Output": 2547,rn    "InTarget": 3033,rn    "OutTarget": 2533rn  }rn]"

我做错了什么?

你实际上得到了正确的输出,老板,我认为你看错了你应该在visual studio中使用Text Visualizer查看输出,所以我认为你的输出是ok的

     DataSet ds = new DataSet();
    DataTable dt;
    DataRow dr;
    DataColumn objstrError;
    DataColumn objBytearr;
    dt = new DataTable();
    objstrError = new DataColumn("strID", Type.GetType("System.String"));
    objBytearr = new DataColumn("strName", Type.GetType("System.String"));
    dt.Columns.Add(objstrError);
    dt.Columns.Add(objBytearr);
    dr = dt.NewRow();
    dr["strID"] = "1";
    dr["strName"] = "Britains got talent";
    dt.Rows.Add(dr);
    string json    =JsonConvert.SerializeObject(dt,Newtonsoft.Json.Formatting.Indented);
     normal intelisense gives:"[rn  {rn    "strID": "1",rn          "strName": "Britains got talent"rn  }rn]"
using TextVisualizer if you see it then looks like this:[
{
"strID": "1",
"strName": "Britains got talent"
 }
]

相关内容

  • 没有找到相关文章

最新更新