none
How do I output json from a standalone c# page RRS feed

  • Question

  • I have a dictionary on a C#.NET webpage that I do a lookup on to get a piece of data I need , I would then like to output that data as serialized json I thought nievly that I could just output it using reponse.write like below

    but this code

    string value;

    var result = dict.TryGetValue(thename, out value);

    Response.Write ("{\"transit\": \"" + value + "\"}");

    outputs

    <html>
    <body>

    {"transit": "15008"}

    </body>
    </html>

    and jquery cant read it

        <script>
        $.getJSON('https://test.here.com/files/gettransit.aspx, function(data) {
            var text = 'Transit: ${data.transit}' ;        
            alert(text);
            $(".mypanel").html(text);
        });
        </script>
    how do I get my aspx page to output json

     

     

     

     

     


    <

    html>

    <

    body>


     

     

     

    {"transit": "15008"}

     

     


    </

    body>

    </

    html>

    • Moved by CoolDadTx Thursday, September 12, 2019 5:26 PM ASP.NET related
    Thursday, September 12, 2019 5:18 PM

Answers

  • string json = "{\"transit\":\"" + value + "\"}";
    Response.Clear();
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(json);
    Response.End();

    • Marked as answer by robMerritt Wednesday, September 18, 2019 1:35 PM
    Wednesday, September 18, 2019 1:35 PM

All replies

  • Please post questions related to web development in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Thursday, September 12, 2019 5:26 PM
  • string json = "{\"transit\":\"" + value + "\"}";
    Response.Clear();
    Response.ContentType = "application/json; charset=utf-8";
    Response.Write(json);
    Response.End();

    • Marked as answer by robMerritt Wednesday, September 18, 2019 1:35 PM
    Wednesday, September 18, 2019 1:35 PM