开发者

JSON String formed improperly using Jayrock in .NET

I am trying to return a JSON object from an aspx page using Jayrock.JSON. My code for writing it out is this:

using (JsonTextWriter writer = new JsonTextWriter(Response.Output))
    {
        writer.WriteStartObject();
        for (int i = 0; i < rdr.FieldC开发者_如何学Pythonount; i++)
        {
            writer.WriteMember(rdr.GetName(i).ToString());
            writer.WriteString(rdr[i].ToString());
        }
        writer.WriteEndObject();
    }

This is inside of an rdr.Read() loop.

The outputted JSON looks like this: (though I added the line breaks manually)

{
"BugID":"1087",
"AddedBy":"",
"BugDate":"5/2/2010 9:45:34 AM",
"BugTitle":"/admin/ajax_thirdpartylog.asp",
"Classify":""
,"ErrPage":"/admin/ajax_thirdpartylog.asp",
"StoreID":"71",
"UserID":"15438",
"ErrDesc":"Type mismatch: 'formatnumber'",
"ErrDump":"*** VARIABLES DUMP ***\r\n\r\n*** Form Variables ***\r\n\r\ncalmonth : 8\r\ncalmonth2 : 8\r\nfromdate : 8/1/2009\r\ncalyear : 2009\r\ntodate : 8/31/2009\r\ncalyear2 : 2009\r\nr : 978402\r\nthirdtype : 1\r\nButton : Generate Third Party Log\r\n\r\n*** Query String Variables ***\r\n\r\n\r\n\r\n*** REPORT END ***\r\n",
"ErrLine":"74",
"DateFixed":"",
"Counter":"16",
"AssignTo":""
 }
 {
"BugID":"1086",
"AddedBy":"",
"BugDate":"5/1/2010 11:58:54 PM",
"BugTitle":"/admin/Charts/monthsales_s.asp",
"Classify":"",
"ErrPage":"/admin/Charts/monthsales_s.asp",
"StoreID":"402",
"UserID":"141928",
"ErrDesc":"Script timed out",
"ErrDump":"*** VARIABLES DUMP ***\r\n\r\n*** Form Variables ***\r\n\r\n\r\n*** Query String Variables ***\r\n\r\nmonth1 : 9/1/2009\r\nr : 75333803\r\n\r\n\r\n*** REPORT END ***\r\n",
"ErrLine":"0",
"DateFixed":"",
"Counter":"",
"AssignTo":""
 }

I'm not really sure what I'm doing wrong, but on my page reading this JSON, when I try to do .evalJSON (using prototypejs) I get errors saying that the JSON is malformed. Can anyone advise me what to change?


This:

"AssignTo":"" 
 } 
 { 

is the invalid JSON. You can a string to see if it's valid JSON at JSON Lint. I'm not sure what this should be like, but an empty object would be like this (don't need "", brackets reversed, missing comma):

"AssignTo": 
 { 
 }, 


The problem is that you are writing multiple JSON objects whereas what you are probably trying to do is produce a JSON array of JSON objects. Given your code snippet, I'm assuming rdr holds some IDataReader implementation like SqlDataReader. If that's true then you need to modify your code to start and end a JSON array around the outer read loop, as follows:

using (JsonTextWriter writer = new JsonTextWriter(Response.Output))
{        
    writer.WriteStartArray();
    while (rdr.Read())
    {
        writer.WriteStartObject();
        for (int i = 0; i < rdr.FieldCount; i++)
        {
            writer.WriteMember(rdr.GetName(i).ToString());
            writer.WriteString(rdr[i].ToString());
        }
        writer.WriteEndObject();
    }
    writer.WriteEndArray();
}

Jayrock will automatically delimit each JSON object value with a comma (,) when inside a JSON array so now the output should resemble the following and valid JSON:

[
    {
        "BugID":"1087",
        "AddedBy":"",
        "BugDate":"5/2/2010 9:45:34 AM",
        "BugTitle":"/admin/ajax_thirdpartylog.asp",
        "Classify":""
        ,"ErrPage":"/admin/ajax_thirdpartylog.asp",
        "StoreID":"71",
        "UserID":"15438",
        "ErrDesc":"Type mismatch: 'formatnumber'",
        "ErrDump":"*** VARIABLES DUMP ***\r\n\r\n*** Form Variables ***\r\n\r\ncalmonth : 8\r\ncalmonth2 : 8\r\nfromdate : 8/1/2009\r\ncalyear : 2009\r\ntodate : 8/31/2009\r\ncalyear2 : 2009\r\nr : 978402\r\nthirdtype : 1\r\nButton : Generate Third Party Log\r\n\r\n*** Query String Variables ***\r\n\r\n\r\n\r\n*** REPORT END ***\r\n",
        "ErrLine":"74",
        "DateFixed":"",
        "Counter":"16",
        "AssignTo":""
    },
    {
        "BugID":"1086",
        "AddedBy":"",
        "BugDate":"5/1/2010 11:58:54 PM",
        "BugTitle":"/admin/Charts/monthsales_s.asp",
        "Classify":"",
        "ErrPage":"/admin/Charts/monthsales_s.asp",
        "StoreID":"402",
        "UserID":"141928",
        "ErrDesc":"Script timed out",
        "ErrDump":"*** VARIABLES DUMP ***\r\n\r\n*** Form Variables ***\r\n\r\n\r\n*** Query String Variables ***\r\n\r\nmonth1 : 9/1/2009\r\nr : 75333803\r\n\r\n\r\n*** REPORT END ***\r\n",
        "ErrLine":"0",
        "DateFixed":"",
        "Counter":"",
        "AssignTo":""
    }
]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜