JSON String在.NET中使用Jayrock不正确地形成

时间:2010-04-05 15:06:19

标签: json jayrock

我正在尝试使用Jayrock.JSON从aspx页面返回JSON对象。 我写出来的代码是:

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

这是在rdr.Read()循环中。

输出的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":""
 }

我不确定我做错了什么,但是在我的页面上阅读这个JSON,当我尝试做.evalJSON(使用prototypejs)时,我得到的错误是JSON格式错误。 任何人都可以建议我改变什么?

2 个答案:

答案 0 :(得分:1)

此:

"AssignTo":"" 
 } 
 { 

是无效的JSON。你可以在JSON Lint看一个字符串,看看它是否是有效的JSON。我不确定这应该是什么样的,但是一个空对象就是这样(不需要“”,括号反转,缺少逗号):

"AssignTo": 
 { 
 }, 

答案 1 :(得分:1)

问题是您正在编写多个JSON对象,而您可能要做的是生成JSON对象的JSON数组。鉴于您的代码段,我假设rdr包含一些IDataReader实现,如SqlDataReader。如果这是真的那么你需要修改你的代码来开始和结束外部读取循环周围的JSON数组,如下所示:

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会在JSON数组中使用逗号(,)自动分隔每个JSON对象值,所以现在输出应该类似于以下有效的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":""
    }
]