Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Claushingebjerg 936 posts 2571 karma points
    Sep 04, 2015 @ 11:39
    Claushingebjerg
    0

    Rendering json as html

    Seing as json seems to be the data format of choice these days :) i guess it should be very simple to render json to html in razor... I just cant seem to find a way to do it. Say i have a simple chunk of json like this:

        [{  
          "skole":"skole1",
          "link":"link1",
       },
       {  
          "skole":"skole2",
          "link":"link2",
       }]
    

    How would one go about rendering this data in, lets say, an unordered list?:

    <ul>
      <li>skole1, link1</li>
      <li>Skole2, link2</li>
    </ul>
    
  • Steve Morgan 1346 posts 4453 karma points c-trib
    Sep 05, 2015 @ 10:23
    Steve Morgan
    0

    Hi Claushingebjerg,

    I tend to use Newtonsoft's JSON.Net and JArrays. This is a dynamic example - you might want to create a relevant class and parse it .

    @using Newtonsoft.Json.Linq
    
    @{
        string jsonTest = @"[{
          ""skole"":""skole1"",
          ""link"":""link1"",
       },
       {
          ""skole"":""skole2"",
          ""link"":""link2"",
       }]";
    
        var jArray = JArray.Parse(jsonTest);
    
        if (jArray.Count > 0)
        {
            <ul>
                @foreach (dynamic x in jArray)
                {
    
                <li>@x.skole, @x.link</li>
                }
            </ul>
        }
    }
    

    More info here: http://www.newtonsoft.com/json/help/html/QueryJsonDynamic.htm

Please Sign in or register to post replies

Write your reply to:

Draft