1

I am trying to generate JSON. The data is there but it is my understanding that I need to serialize it for it to work. I'm a beginner in C# so the documentation on the newtonsoft site is a bit foreign to me. Here is my C# code.

public partial class Pages_ChartTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public static IEnumerable<procReportSalesCountsResult> jsonData()
    {
        using (OpsDBDataContext dc = new OpsDBDataContext())
        {     
            return dc.ReportSalesCounts().ToList();          
        }   
    }
}

What is my final step to serialize. I pulled this data in using Linq To SQL in ASP. Thank you all.

aaron
  • 43
  • 1
  • 8
  • What happens now if you make a `GET` request? – Matt Burland Sep 19 '14 at 20:04
  • If I make a get request in my JS file I get a 500 error. If I make a POST request from the same file and output to the console I get {"d":[{"__type":"procReportSalesCountsResult","MonthID":null,"SoldCount":74,"MonthName":"Jan"},{"__type":"procReportSalesCountsResult","MonthID":null,"SoldCount":74,"MonthName":"Feb"},{"__type":"procReportSalesCountsResult","MonthID":null,"SoldCount":114,"MonthName":"Mar"} – aaron Sep 19 '14 at 20:08
  • aside from some weird encoding (in `74` for the month `Feb`) what you pasted there (that I'm hoping wasn't in the original) and a missing `]}` at the end (which I hope was in the original) that looks like valid JSON to me. Try pasting it into http://jsonlint.org/ – Matt Burland Sep 19 '14 at 20:38
  • It shows valid. Do I not need to serialize? – aaron Sep 19 '14 at 20:44
  • Unless you want it serialized some other way, no. You've told it you want json with the `ResponseFormat = ResponseFormat.Json`. – Matt Burland Sep 19 '14 at 20:46
  • You've done enough to return JSON. You don't even need the ScriptMethod attribute. You do, as you have found, need to POST the request and request JSON. A working JavaScript example (the C# is for an asmx service which is ever so slightly different) is here, probably too late to help you but might help others. http://stackoverflow.com/a/16335022/397817 – Stephen Kennedy May 21 '15 at 13:35

1 Answers1

1

JSON isn't natively supported by .NET until 4.5. Download the Nuget package JSON.net by Newtonsoft, right from the VS package manager.

http://james.newtonking.com/json

public static string jsonData() {
    var buffer = null;
    using (OpsDBDataContext dc = new OpsDBDataContext()) {
        buffer = dc.ReportSalesCounts().ToList();
    } 
    return JsonConvert.SerializeObject(buffer);
}
Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Mark Dineen
  • 390
  • 3
  • 10
  • I did that. I brought in the newtonsoft namespace. Thank you for your reply. I just don't know how to implement it in my code – aaron Sep 19 '14 at 20:14
  • `JsonConvert.SerializeObject(object)` returns string – Mark Dineen Sep 19 '14 at 20:24
  • Thank you Mark. So like this? Sorry I'm just learning. public static IEnumerable jsonData() { using (OpsDBDataContext dc = new OpsDBDataContext()) { return dc.ReportSalesCounts().ToList(); } JsonConvert.SerializeObject(dc.ReportSalesCounts().ToList()); } – aaron Sep 19 '14 at 20:42
  • Your comment isn't annotated as code, so a little hard to follow. It looks to my eye that you are creating a public static method to return an ienumerable, then in your using statement you're terminating it by returning the list. If I'm reading the above correctly, you're not going to see that json data. Perhaps: try instantiating an ienumerable above your using statement, set it equal to your counrs().ToList() in the using, then return the SerializeObject call's response. – Mark Dineen Sep 19 '14 at 20:46
  • There, added implementation above. – Mark Dineen Sep 19 '14 at 20:51
  • Thanks Mark..again please excuse my ignorance. So this would replace the entire `public static IEnumerable<> jsonData()` that I had before? Or does your code go inside of that. I can't get my head around the comment explanation. Thank you again. – aaron Sep 19 '14 at 20:59
  • It's telling me that Cannot assign to an implicitly-typed local variable. – aaron Sep 19 '14 at 21:01
  • @aaron: As I commented on your question, it appears you code is serializing anyway, so I wouldn't even bother unless you have some reason for wanting more control over the exact way it get serialized (and even then, there are better ways to handle it). – Matt Burland Sep 19 '14 at 21:03
  • For the error, just replace `var` with `IEnumerable` and it should be okay. – Matt Burland Sep 19 '14 at 21:03
  • I need it serialized as I'm using this data for FlotCharts. I'm getting an error now but the data looks perfect. "Cannot use 'in' operator to search for '321' in" – aaron Sep 19 '14 at 21:13
  • Sorry but this answer whilst a common misconception is incorrect. Page methods support JSON out of the box courtesy of the JavaScriptSerializer which has been in the framework since 3.5. Provided the web request is for JSON the OP's page method already returns it as indeed the JSON response in the comments show! The problem would appear to be in his JavaScript not server side. Although undeniably excellent Newtonsoft is simply not needed here. http://stackoverflow.com/a/16335022/397817 – Stephen Kennedy May 21 '15 at 13:31