ASP.NET Wep API 2无法返回正确的JSON字符串

时间:2018-07-25 16:58:09

标签: c# asp.net json asp.net-web-api

我正在使用ASP.NET Web API 2将数据表作为JSON字符串返回。 但是响应字符串不是正确的JSON格式。这是Web API的输出:

"[{\"RESERVATIONDATE\":\"20180725\",\"SCHEDULE_CODE\":619675,\"DEPARTURE_SEQ\":1,\"ARRIVAL_SEQ\":3,\"SCHEDULE_ROUTE\":9107,\"SCHEDULE_ROUTE_NAME\":\"LHR-SWL-MTN\",\"SCHEDULE_DEPARTURE_TIME\":\"2215\",\"SCHEDULE_ARRIVAL_TIME\":\"0330\",\"SCHEDULE_TIMECODE\":\"1\",\"SCHEDULE_BUSTYPE\":14,\"SCHEDULE_EXTRA\":\"N\",\"DEPARTURE_NAME\":\"LAHORE\",\"ARRIVAL_NAME\":\"MULTAN\",\"FARE_FARE\":750.0,\"BUSTYPE\":\"Super Luxury (Discount)\",\"BUSTYPE_NAME\":\"DISC\",\"TDAY\":0.0,\"RTIME\":73.0,\"BUSTYPE_SEATS\":45,\"STAFF_SEAT\":2,\"AVAILABLE\":31.0,\"RESERVED\":12.0,\"DELIVERED\":1.0,\"SCHEDULE_REMAIN\":\"     \",\"CURRENT_OPEN\":\"N\",\"TRIP_STATUS\":\"DROP\",\"DEPARTURE\":\"LHR\",\"ARRIVAL\":\"MTN\",\"FARE_Y\":0.0}]"

但是我希望以这种格式回复:

{
    "Success": true,
    "Response": [
        {
            "RESERVATIONDATE": "20180725",
            "SCHEDULE_CODE": 619675,
            "DEPARTURE_SEQ": 1,
            "ARRIVAL_SEQ": 3,
            "SCHEDULE_ROUTE": 9107,
            "SCHEDULE_ROUTE_NAME": "LHR-SWL-MTN",
            "SCHEDULE_DEPARTURE_TIME": "2215",
            "SCHEDULE_ARRIVAL_TIME": "0330",
            "SCHEDULE_TIMECODE": "1",
            "SCHEDULE_BUSTYPE": 14,
            "SCHEDULE_EXTRA": "N",
            "DEPARTURE_NAME": "LAHORE",
            "ARRIVAL_NAME": "MULTAN",
            "FARE_FARE": 750,
            "BUSTYPE": "Super Luxury (Discount)",
            "BUSTYPE_NAME": "DISC",
            "TDAY": 0,
            "RTIME": 60,
            "BUSTYPE_SEATS": 45,
            "STAFF_SEAT": 2,
            "AVAILABLE": 31,
            "RESERVED": 12,
            "DELIVERED": 1,
            "SCHEDULE_REMAIN": "     ",
            "CURRENT_OPEN": "N",
            "TRIP_STATUS": "DROP",
            "DEPARTURE": "LHR",
            "ARRIVAL": "MTN",
            "FARE_Y": 0
        } ]
}

这是我的代码,用于将数据表序列化为JSON:

JavaScriptSerializer serializer = new JavaScriptSerializer();
DataTable dt =await oracleManager.GetSchedule(Departure, Arrival, Date);
if (dt.Rows.Count > 0)
{
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    Dictionary<string, object> row;
    foreach (DataRow dr in dt.Rows)
    {
       row = new Dictionary<string, object>();
       foreach (DataColumn col in dt.Columns)
        {
          row.Add(col.ColumnName, dr[col]);
        }
      rows.Add(row);
   }
   return await Task.FromResult(serializer.Serialize(rows));
}

这是我在配置文件中添加的媒体格式化程序:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

如何解决此问题并获得所需的格式?

2 个答案:

答案 0 :(得分:1)

您实际上不需要在WebApi中进行任何手动序列化。如果只是直接通过控制器方法返回字典,则应该获得正确的JSON输出。

WebApi将自动将其转换为JSON。

答案 1 :(得分:0)

您需要创建一个类型来保存表和成功消息,或者您使用匿名对象

DataTable dt =await oracleManager.GetSchedule(Departure, Arrival, Date);
if (dt.Rows.Count > 0)
{
    List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
    Dictionary<string, object> row;
    foreach (DataRow dr in dt.Rows)
    {
       row = new Dictionary<string, object>();
       foreach (DataColumn col in dt.Columns)
        {
          row.Add(col.ColumnName, dr[col]);
        }
      rows.Add(row);
   }

   //add this line
   var output = new { Success = true, Response = rows };
   // then serialize the new object
   return await Task.FromResult(output);
}