1

我正在尝试使用把手(模板)通过 ajax 组织获取的 jSon 对象

这是对象的样子,PHP:

[0] => Url Object
    (
        [url_id] => 1
        [url] => www.stackoverflow.com
        [tags] => Array
            (
                [0] => Tag Object
                    (
                        [tag_id] => 1
                        [tag_name] => programming
                    )

                [1] => Tag Object
                    (
                        [tag_id] => 2
                        [tag_name] => questions
                    )

            )

    )

[1] => Url Object
    (
        [url_id] => 2
        [url] => www.twitter.com
        [tags] => Array
            (
                [0] => Tag Object
                    (
                        [tag_id] => 3
                        [tag_name] => messaging
                    )

            )

    )

我可以通过以下方式从 Object[0] 和 object [1] 获取 url_id 和 url:(JQUERY)

$.getJSON(url,function(data){

                    Bookmarks = $.map(data,function(bookmark){ //segundo param = callback function

                                    return{
                                      url_id: bookmark.url_id,
                                      url: bookmark.url,
                                      tags: bookmark.tags
                                          }
                                                                  });

                var contenedor = $('#scriptmark').html();
                var template = Handlebars.compile(contenedor);
                var i = 0;
                while( i < Bookmarks.length){
                $('#sites').append(template(Bookmarks[i]));
                i++;}
        });

最后,这就是我的模板的样子:

<script id="script" type="text/x-handlebars-template">


            <div class="site">
            Site: {{url}} Tags: {{tag}}
            </div>


</script>

我正在尝试弄清楚如何获取这些标签,因为当我进行打版时,我显然会返回 Object

4

1 回答 1

0

也许是这样的?

 Bookmarks = $.map(data,
     function(bookmark){ //segundo param = callback function
         var tagstring="";
         $.each(bookmark.tags, function(index, value){
              if(index > 0){tagstring+=", ";}
              tagstring+=value.tag_id+"-->"+value.tag_name;
         }
         return
         {
                url_id: bookmark.url_id,
                url: bookmark.url,
                tags: tagstring
         }
      }
 );
于 2013-02-15T12:25:30.380 回答