You must Sign In to post a response.
  • Category: ASP.NET

    Get data from Json URL in asp.net

    I have json URL and i want to implement in the Asp.NET https://openexchangerates.org/api/latest.json?app_id=0277d31956db4d57af7207ca1ab782a5&symbols=MYR,EUR,AED,CAD

    Result in web page is:
    {
    "disclaimer": "Usage subject to terms: https://openexchangerates.org/terms",
    "license": "https://openexchangerates.org/license",
    "timestamp": 1517025604,
    "base": "USD",
    "rates": {
    "AED": 3.673018,
    "CAD": 1.231755,
    "EUR": 0.804525,
    "GBP": 0.706185
    }
    }

    I want to display the above result in asp.net page. I tried the code as below but nothing to shown in the page.
    My code as below:
    <body>
    <form id="form1" runat="server">
    <script type = "text/javascript" language = "javascript">
    $.get('https://openexchangerates.org/api/latest.json', {app_id: 'Y0277d31956db4d57af7207ca1ab782a5', symbols: 'QAR,RUB,SEK'}, function(data) {
    var text = 'Result: ${ data.rates.SEK}';
    $(".mypanel").html(text);
    });
    </script>
    </form>
    </body>
  • #769482
    using System.Net;
    using Newtonsoft.Json;

    // ...

    private static T _download_serialized_json_data<T>(string url) where T : new() {
    using (var w = new WebClient()) {
    var json_data = string.Empty;
    // attempt to download JSON data as a string
    try {
    json_data = w.DownloadString(url);
    }
    catch (Exception) {}
    // if string with JSON data is not empty, deserialize it to class and return its instance
    return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject<T>(json_data) : new T();
    }
    }
    http://www.cetpainfotech.com/technology/Dot-net-training

  • #769492
    You must convert the response into Json and then append the data in your view. Below is some piece of code

    [HttpGet]
    public async Task<ActionResult> Printer()
    {
    string url = "https://localhost/api/test/";
    HttpResponseMessage responseMessage = await client.GetAsync(url);
    if (responseMessage.IsSuccessStatusCode)
    {
    var responseData = responseMessage.Content.ReadAsStringAsync().Result;
    var campusName = JsonConvert.DeserializeObject<List<OrgCampus>>(responseData);
    cs.getCampus = campusName;
    return View(cs);
    }
    return View("Error");
    }

    -----------------------------------------------------------------------------
    Regards,
    Gopi A.
    +91 9894315571
    Skype:gopi.net
    http://asaigopi-dotnet.blogspot.in/


  • Sign In to post your comments