JAVAScriptSerilized JSON String在fiddler上显示无效消息

时间:2016-04-18 09:14:15

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

我正在构建一个WEB API,它应该从SQL Server数据库中检索事件列表,并在JSON中为客户端创建这些对象的响应

事件对象:

public class EventsController : ApiController
{
    private string JSONResponse = "";

    public string getEvents()
    {
        //Create class the haandles all DB related data
        DBConnect db = new DBConnect();

        //Creates List of Event Objects
        List<OCEvents> events = new List<OCEvents>();

        //get latest events posted
        db.getOCEvents(events);

        //Serialize event objects to JSON
        JSONResponse = new JavaScriptSerializer().Serialize(events);

        //return JSON Response
        return JSONResponse;
    }
}

用于检索事件JSON数据的简单Web API控制器

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcREFOT05FXERvY3VtZW50c1xCaXpcTW9iaWxlIEFwcGxpY2F0aW9uIERldmVsb3BtZW50IERvY3VtZW50c1xQcm9qZWN0c1xPdXJDaHVyY2hBUElcT3VyQ2h1cmNoQVBJXGFwaVxldmVudHM=?=
X-Powered-By: ASP.NET
Date: Mon, 18 Apr 2016 08:58:41 GMT
Content-Length: 1061

"[{\"event_id\":5,\"event_date\":\"13 January 2016\",\"event_time\":\"12:00\",\"event_location\":\"South Africa,North West,Rustenburg,Church\",\"event_name\":\"Imposition of Ashes\",\"event_image\":\"\",\"event_description\":\"Come anytime from 12:00 noon to 1:00 p.m for quiet meditation and prayer. Clergy will be available for the imposition of ashes.\"},{\"event_id\":7,\"event_date\":\"13 January 2016\",\"event_time\":\"19:00\",\"event_location\":\"South Africa,North West,Rustenburg,Church\",\"event_name\":\"Ash Wednesday Service\",\"event_image\":\"\",\"event_description\":\"Join us as we mark the begining of the season off Lent with a traditional Ash Wednesday service ,including imposition of ashes.\"},{\"event_id\":1,\"event_date\":\"08 January 2016\",\"event_time\":\"14:00\",\"event_location\":\"South Africa,North West,Rustenburg,Church\",\"event_name\":\"Special Worship Service\",\"event_image\":\"\",\"event_description\":\"Rev. Dr Amy Bulter, preaching Sermon--\\u0027Shine, Jesus,Shine\\u0027hebrew Scripture Lesson--Exodus 34:29-35.\"}]"

这是来自事件控制器的原始回发(响应)

<input type="checkbox" name="ckflood[]" value="VerySafe"> Very Safe
<input type="checkbox" name="ckflood[]" value="Safe"> Safe
<input type="checkbox" name="ckflood[]" value="Average"> Average
<input type="checkbox" name="ckflood[]" value="Heavy"> Heavy
<input type="checkbox" name="ckflood[]" value="VeryHeavy"> Very Heavy

当我打开JSON标签

时,我的问题是这个错误

Fiddler screenshot with inspector & json tab open

2 个答案:

答案 0 :(得分:0)

我终于明白了。问题是getEvents方法返回的是字符串而不是HTTP Response对象。因此,我没有返回JSON字符串,而是使用了它。

public HttpResponseMessage getEvents()
    {
        //Create class the haandles all DB related data
        DBConnect db = new DBConnect();

        //Creates List of Event Objects
        List<OCEvents> events = new List<OCEvents>();

        //get latest events posted
        db.getOCEvents(events);

        //Serialize event objects to JSON
        JSONResponse = new JavaScriptSerializer().Serialize(events);

        var response = new HttpResponseMessage()
        {
            Content = new StringContent(JSONResponse)

        };
        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        //return JSON Response
        return response;
    }

答案 1 :(得分:0)

您可以让系统根据用户原始请求中的接受标头进行JSON或XML创建。

public HttpResponseMessage getEvents()
    {
        //Create class the haandles all DB related data
        DBConnect db = new DBConnect();

        //Creates List of Event Objects
        List<OCEvents> events = new List<OCEvents>();

        //get latest events posted
        db.getOCEvents(events);

        return Request.CreateResponse(HttpStatusCode.OK, events);
    }

如果你想强制使用JSON,那么在你的WepApiConfig中删除XML格式化程序。

    //Remove XML so only JSON is returned
    GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);