Search code examples
javascriptjqueryjsontemplatesjquery-templates

Read Json using Jquery template For loop


I am trying to render value from below Json using Jquery template for loop, but not getting any values out of Json. Below is my Json:

    {
   "CommentListClass":[
      {
         "Author":"Attis1",
         "CommentText":"Really goo"
      },
      {
         "Author":"joshar1",
         "CommentText":"My picture"
      },

    }

My Jquery template is:

<script type="text/x-jsrender" id="tempRecentComments">
              {{for List}}
                <div class="item">
                    <div class="item-content">
                       <h3>{{:CommentListClass.Author}}</h3>
                    </div>
               </div>
                 {{/for}}

                </script>

Solution

  • Try this,

    {{for CommentListClass}}
          <div class="item">
             <div class="item-content">
                <h3>{{:Author}}</h3>
             </div>
          </div>
    {{/for}}
    

    Use data like,

    [{
       "CommentListClass": [{
           "Author": "Test",
           "CommentText": "Really goo"
       }, {
           "Author": "joshar1",
           "CommentText": "My picture"
       }]
    }];
    

    Test on jsviews