如何从WebAPI服务的JSON输出中删除回车符?

时间:2018-12-21 04:03:07

标签: c# asp.net asp.net-web-api kendo-ui kendo-ui-grid

我创建了一个Web API服务,该服务输出JSON,供KendoUI数据网格使用。麻烦之处在于:KendoUI数据网格对它使用的JSON代码非常挑剔。它不能有任何类型的回车/换行符。它必须是一大块未格式化的JSON。

我们与Telerik的某人一起工作,他们建立了某种代理(https://cors.io/?http://js01.consultwithus.us/odata/vw_FilesToBeProcessed_Dashboard),但这只是一个创可贴。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        // enable CORS
        config.EnableCors();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // we must set odata to version 2
        var vers = new Version(2, 0);

        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.DataServiceVersion = vers;
        builder.MaxDataServiceVersion = vers;
        builder.EntitySet<vw_FilesToBeProcessed_Dashboard>("vw_FilesToBeProcessed_Dashboard");
        config.Routes.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());
    }
}

我希望我的输出看起来像this,但是现在它看起来像this


更新12/28/2018

这是我的控制器的代码:

[EnableCors(origins: "http://js01.consultwithus.us", headers: "*", methods: "*")]
public class vw_FilesToBeProcessed_DashboardController : ODataController
{
    private CSS_DevEntities db = new CSS_DevEntities();

    // GET: odata/vw_FilesToBeProcessed_Dashboard
    [EnableQuery]
    public IQueryable<vw_FilesToBeProcessed_Dashboard> Getvw_FilesToBeProcessed_Dashboard()
    {
        return db.vw_FilesToBeProcessed_Dashboard;
    }

    // GET: odata/vw_FilesToBeProcessed_Dashboard(5)
    [EnableQuery]
    public SingleResult<vw_FilesToBeProcessed_Dashboard> Getvw_FilesToBeProcessed_Dashboard([FromODataUri] int key)
    {
        return SingleResult.Create(db.vw_FilesToBeProcessed_Dashboard.Where(vw_FilesToBeProcessed_Dashboard => vw_FilesToBeProcessed_Dashboard.Files_PK == key));
    }

    // PUT: odata/vw_FilesToBeProcessed_Dashboard(5)
    public IHttpActionResult Put([FromODataUri] int key, Delta<vw_FilesToBeProcessed_Dashboard> patch)
    {
        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        vw_FilesToBeProcessed_Dashboard vw_FilesToBeProcessed_Dashboard = db.vw_FilesToBeProcessed_Dashboard.Find(key);
        if (vw_FilesToBeProcessed_Dashboard == null)
        {
            return NotFound();
        }

        patch.Put(vw_FilesToBeProcessed_Dashboard);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!vw_FilesToBeProcessed_DashboardExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(vw_FilesToBeProcessed_Dashboard);
    }

    // POST: odata/vw_FilesToBeProcessed_Dashboard
    public IHttpActionResult Post(vw_FilesToBeProcessed_Dashboard vw_FilesToBeProcessed_Dashboard)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.vw_FilesToBeProcessed_Dashboard.Add(vw_FilesToBeProcessed_Dashboard);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateException)
        {
            if (vw_FilesToBeProcessed_DashboardExists(vw_FilesToBeProcessed_Dashboard.Files_PK))
            {
                return Conflict();
            }
            else
            {
                throw;
            }
        }

        return Created(vw_FilesToBeProcessed_Dashboard);
    }

    // PATCH: odata/vw_FilesToBeProcessed_Dashboard(5)
    [AcceptVerbs("PATCH", "MERGE")]
    public IHttpActionResult Patch([FromODataUri] int key, Delta<vw_FilesToBeProcessed_Dashboard> patch)
    {
        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        vw_FilesToBeProcessed_Dashboard vw_FilesToBeProcessed_Dashboard = db.vw_FilesToBeProcessed_Dashboard.Find(key);
        if (vw_FilesToBeProcessed_Dashboard == null)
        {
            return NotFound();
        }

        patch.Patch(vw_FilesToBeProcessed_Dashboard);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!vw_FilesToBeProcessed_DashboardExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(vw_FilesToBeProcessed_Dashboard);
    }

    // DELETE: odata/vw_FilesToBeProcessed_Dashboard(5)
    public IHttpActionResult Delete([FromODataUri] int key)
    {
        vw_FilesToBeProcessed_Dashboard vw_FilesToBeProcessed_Dashboard = db.vw_FilesToBeProcessed_Dashboard.Find(key);
        if (vw_FilesToBeProcessed_Dashboard == null)
        {
            return NotFound();
        }

        db.vw_FilesToBeProcessed_Dashboard.Remove(vw_FilesToBeProcessed_Dashboard);
        db.SaveChanges();

        return StatusCode(HttpStatusCode.NoContent);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool vw_FilesToBeProcessed_DashboardExists(int key)
    {
        return db.vw_FilesToBeProcessed_Dashboard.Count(e => e.Files_PK == key) > 0;
    }
}

我有Visual Studio根据我的EDM为我架设的支架。

3 个答案:

答案 0 :(得分:0)

我相信您想删除JSON字符串中的格式。您可以使用Newtonsoft.Json

   var jtoken = JToken.Parse(strValue);
   var formattedValue = jtoken.ToString(Newtonsoft.Json.Formatting.None);

答案 1 :(得分:0)

您没有显示正在创建JSON的控制器方法。但是我可以猜到你有某种对象。既然您提到了C#,我也会投票支持Newtonsoft,但建议使用JsonConvert.SerializeObject()作为格式化程序。 https://www.newtonsoft.com/json/help/html/SerializingJSON.htm

类似的东西:

var myResult = GetMyResult();
var response = JsonConvert.SerializeObject(myResult);
return response;

答案 2 :(得分:0)

此行为由JsonSerializerSettings.Formatting设置控制。根据{{​​3}},默认值为None,表示没有换行或缩进。

因此,如果您正在看到新行并在输出中缩进,则该设置将更改为Newtonsoft.Json.Formatting.Indented。发生了两件事之一:

  1. 该设置是在该特定控制器/操作中设置的,或者
  2. 默认设置已更改。显然,它没有在Register方法中进行更改,但是可以通过设置JsonConvert.DefaultSettings随时进行更改。

基本上,在整个解决方案中搜索对Newtonsoft.Json.Formatting.Indented的任何引用。

更新:您正在使用ODataController,它将更改所使用的序列化程序。因此,它不再使用Json.NET进行序列化。 the documentation关于如何使用Json.NET。该示例仅显示了 de 串行化,因此您也必须使该类用于序列化。有点烦人。