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.

6
  • What happens now if you make a GET request? Commented Sep 19, 2014 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"} Commented Sep 19, 2014 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 jsonlint.org Commented Sep 19, 2014 at 20:38
  • It shows valid. Do I not need to serialize? Commented Sep 19, 2014 at 20:44
  • Unless you want it serialized some other way, no. You've told it you want json with the ResponseFormat = ResponseFormat.Json. Commented Sep 19, 2014 at 20:46

1 Answer 1

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);
}
Sign up to request clarification or add additional context in comments.

11 Comments

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
JsonConvert.SerializeObject(object) returns string
Thank you Mark. So like this? Sorry I'm just learning. public static IEnumerable<procReportSalesCountsResult> jsonData() { using (OpsDBDataContext dc = new OpsDBDataContext()) { return dc.ReportSalesCounts().ToList(); } JsonConvert.SerializeObject(dc.ReportSalesCounts().ToList()); }
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.
There, added implementation above.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.