convert json to html in .NET website

html .net json json.net

109 просмотра

1 ответ

I'm making a .net website and I have a url which returns json but I don't know how to get that to html.

The code i have so far is from Json.net

 using (var webClient = new System.Net.WebClient())
{
    var json = webClient.DownloadString("http://localhost:8080/projecten/api/leerlingen");

}

But I'm stuck here as how to use this url to show something on my web page.

I also tried with javascript but that doesnt show anything

<div id="id01"></div>

<script>
var xmlhttp = new XMLHttpRequest();
var url = "http://localhost:8080/projecten/api/leerlingen";

xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

        myFunction(xmlhttp.responseText);
    }
}
xmlhttp.open("GET", url, true);
xmlhttp.send();

function myFunction(response) {
    var arr = JSON.parse(response);
    var i;
    var out = "<table>";

    for(i = 0; i < arr.length; i++) {
        out += "<tr><td>" +
        arr[i].inschrijvingsNummer +
        "</td><td>" +
        arr[i].naam +
        "</td><td>" +
        arr[i].email +
        "</td><td>" +
        arr[i].evaluatieNummer +
        "</td></tr>";
    }
    out += "</table>";
    document.getElementById("id01").innerHTML = out;
}
</script>

This is what the json returns:

[{"inschrijvingsNummer":"0001","naam":"Joske Vermeulen","email":"joske.vermeulen@hotmail.be","evaluatieNummer":"270"},{"inschrijvingsNummer":"0002","naam":"Kamiel Spiesses","email":"Kamiel.spiesses@gmail.com","evaluatieNummer":"370"}]
Автор: Burst of Ice Источник Размещён: 08.11.2019 11:19

Ответы (1)


0 плюса

You can load your JSON string in to a Newtonsoft.Json.Linq.JArray object and from there you can iterate through the array, and access the properties.

If you are using webforms you can do this, for example:

Your ASPX.CS:

using System;
using System.Web.UI;
using Newtonsoft.Json.Linq;

namespace YourNamespace {
    public partial class YourPage : Page 
    {
        protected void Page_Load(object sender, EventArgs e) 
        {
            using (var webClient = new System.Net.WebClient())
            {
                var json = webClient.DownloadString("http://localhost:8080/projecten/api/leerlingen");
            }
            rpt.DataSource =  JArray.Parse(json);
            rpt.DataBind();
        }
    }
}

Your ASPX:

<asp:Repeater ID="rpt" runat="server">
    <ItemTemplate>
        <p>
            <strong>Inschrijvings Nummer</strong>: <%# DataBinder.Eval(Container.DataItem, "inschrijvingsNummer") %><br />
            <strong>Naam</strong>: <%# DataBinder.Eval(Container.DataItem, "naam") %><br />
            <strong>Email</strong>: <%# DataBinder.Eval(Container.DataItem, "email") %><br />
            <strong>Evaluatie Nummer</strong>: <%# DataBinder.Eval(Container.DataItem, "evaluatieNummer") %><br />
        </p>
    </ItemTemplate>
</asp:Repeater>
Автор: Andy-Delosdos Размещён: 20.08.2016 10:41
Вопросы из категории :
32x32