views:

134

answers:

1

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.FieldCount; 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?

+1  A: 

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": 
 { 
 }, 
rosscj2533
Any idea how to output that with JayRock.JSON?
Pselus
No, not really unfortunately. What does the definition of `rdr` look like? To me it looks like the problem is that this code: `rdr[i].ToString()` is called and `rdr[i]` is an object, which is not what you are expecting. In that case you'd need another loop, or you could change your method to loop recursively when an object is encountered that way. But I haven't used JayRock.JSON so that's all really just a guess.
rosscj2533
For reference, with JayRock.JSON when you have multiple objects you have to follow this process:`WriteStartObject()WriteMember("SOME UNIQUE ID TO IDENTIFY EACH OBJECT")WriteStartArray()WriteStartObject() // For each objectWriteMembers and WriteStrings of objectWriteEndObject()WriteEndArray()WriteEndObject()`EDIT:I have no idea how to properly format that in a comment.
Pselus