Copied to clipboard

Flag this post as spam?

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


  • Claushingebjerg 939 posts 2574 karma points
    Dec 11, 2014 @ 18:55
    Claushingebjerg
    0

    list nodes as json with parameters

    I need some json for a typeahead feature, and im a bit stucking in some nested listing of parameters.

    I need json of this structure:

    {"items":[
        {
            "name":"Organisationen",
            "url":"/om-uch/organisationen/",
            "value":"Organisationen",
          "tokens": [
                      "i",
                      "need",
              "help"
              ]
        }
    ]}

    I have this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var items = new List<dynamic>(); 
        var thepages = CurrentPage.AncestorOrSelf(1).Descendants("Indholdsside");
        foreach(var page in thepages)
          {
          items.Add(new
              {
                      name = page.Name,
                      url = page.Url,
                      value = page.Name
                 });
          }
      var o = new {
            items = items
        };
        string json = Json.Encode(o);
        Response.ContentType = "application/json";
    }
    @Html.Raw(json)

    I need a bit of help getting the "tokens" rendered inside the json.
    They are a comma separated string from an input field on the doc type.

  • Claushingebjerg 939 posts 2574 karma points
    Dec 11, 2014 @ 20:38
    Claushingebjerg
    0

    Ok, so it wasnt so hard :)

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var items = new List<dynamic>(); 
        var thepages = CurrentPage.AncestorOrSelf(1).Descendants("Praktikplads");
        foreach(var page in thepages)
        {
            var searchterms = page.Name;
            if (page.HasValue("interneSoegeord")){
                searchterms = page.Name + "," + page.interneSoegeord ;
            }   
            items.Add(new
            {
                name = page.Name,
                url = page.Url,
                tokens = searchterms.Split(',')
            });
        }
    
        var o = new {
            items = items
    
        };
    
        string json = Json.Encode(o);
        Response.ContentType = "application/json";
    
    }
    @Html.Raw(json)

    But i have another issue. I need my json to start with a [ and not "{"items":"
    How do i get rid of those {"items": } 

  • Claushingebjerg 939 posts 2574 karma points
    Dec 11, 2014 @ 21:22
    Claushingebjerg
    100

    Another simple solution :)

    string json =Json.Encode(items);
Please Sign in or register to post replies

Write your reply to:

Draft