在HandleBar jsp中迭代数组时访问JSON值

时间:2018-03-02 07:08:15

标签: jsp handlebars.js aem cq5

我有一个带有以下值的json(Sample)。

  public static void Redirect(this HttpResponse response, string url, string target, string windowFeatures)
{

    if ((String.IsNullOrEmpty(target) || target.Equals("_self", StringComparison.OrdinalIgnoreCase)) && String.IsNullOrEmpty(windowFeatures))
    {
        response.Redirect(url);
    }
    else
    {

        Page page = (Page)HttpContext.Current.Handler;

        if (page == null)
        {
            throw new InvalidOperationException("Cannot redirect to new window outside Page context.");
        }
        url = page.ResolveClientUrl(url);

        string script;
        if (!String.IsNullOrEmpty(windowFeatures))
        {
            script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
        }
        else
        {
            script = @"window.open(""{0}"", ""{1}"");";
        }
        script = String.Format(script, url, target, windowFeatures);
        ScriptManager.RegisterStartupScript(page, typeof(Page), "Redirect", script, true);
    }
}

这里我通过迭代使用

显示“结果”
[  
   {  
      "sectionTitle":"LATEST NEWS",
      "language":"ENGLISH",
      "sectionLink":"/content/ml/sample.html",
      "newTab":"",
      "ViewType":"slrr",
      "bgColor":"red",
      "fontColor":"white",
      "logo":"",
      "articleNewTab":"true",
      "Results":[ 
        {  "title":"sample title",
          "description":"sample desc"},
        {
         "title":"sample title1",
          "description":"sample desc1"}
    ],
      "morelinkJson":{  
         "moreLink":"/content/mm/ml/news/just-in.html",
         "moreLinkNewTab":false
      }
   }
]

在这个循环中,我想访问values- sectionTitle,language ...等。 但我不能。 我在这里使用车把jsp。

1 个答案:

答案 0 :(得分:0)

您可以使用{{@root.0.someThing}}模式访问它们。

{{#each this.0.Results}}
      <div class="entry">
        <h1>{{title}}{{@root.0.sectionTitle}}</h1> 
        <div class="body">
          {{@root.0.language}}
          {{description}}
        </div>
      </div>
{{/each}}

请参阅Plunker