如何呈现特定的JSON数据?

时间:2015-04-20 11:18:07

标签: jquery json cordova joomla

我有一个从Joomla生成的JSON提要,我想知道如何使用Jquery在HTML中正确呈现它。

到目前为止,我设法提供了一些数据,但我无法弄清楚如何选择某些礼仪和价值观。

我的JSON

{
    "count": 2,
    "value": {
        "feedUrl": "http://example.com",
        "title": "content",
        "link": "http://example.com",
        "author": "",
        "description": "",
        "type": "application/json",
        "items": [
            {
                "title": "test",
                "link": "http://example.com/index.php/component/content/article/2-uncategorised/5-test?Itemid=101",
                "pubDate": "Mon, 13 Apr 2015 04:23:18 +0000",
                "description": " test test tetst"
            },
            {
                "title": "Module Variations",
                "link": "http://example.com/index.php/component/content/article/2-uncategorised/1-module-variations?Itemid=101",
                "pubDate": "Mon, 02 May 2011 11:45:23 +0000",
                "description": "<p>This theme comes with different <a href=\"http://example.com/index.php/what-we-do/register-your-child\">module styles</a>, badges and icons. For each module you can pick a style and combine it with an icon or badge to create your own unique look. Here is a list of the available options:</p><table class=\"zebra\"><tbody><tr class=\"odd\"><td class=\"bold\">Styles</td><td>Line, Headerline, Box</td></tr><tr><td class=\"bold\">Colors</td><td>Black, Grey, White, Color</td></tr><tr class=\"odd\"><td class=\"bold\">Badges</td><td>Hot, New, Free, Top</td></tr><tr><td class=\"bold\">Icons</td><td>Download, Twitter, Mail, Bubble, Login, Cart</td></tr></tbody></table>"
            }
        ]
    }
}

我的代码

            <div id="json"></div>
                <script>
                $.getJSON( "test.json", function( data ) {


               var items = [];
              $.each( data, function( key, val ) {
                items.push( "<li id='" + key + "'>" + val+ "</li>" );
              });

              $( "<ul/>", {
                "class": "my-new-list",
                html: items.join( "" )
              }).appendTo( "#json" );
            });

        </script>

我想生成这样的东西:

<h1>Title</h1>
<p>Description</p> But from  the  "items" object.

只是更新!

我终于设法使用以下代码:

         <script>
           $.getJSON("http://example.com/logcabin/index.php?option=com_obrss&task=feed&id=3:content&format=json", function( data ) {
          var items = [];
          $.each( data.value.items,function( key, val ) {
            items.push( "<h3 id='" + key + "'>" + val.title + "</h3>" );
            items.push( "<p id='desc'>" + val.description + "</p>" );
          });

          $( "<div/>", {
            "class": "my-new-list",
            html: items.join( "" )
          }).appendTo( "#json" );
        });

       </script>

我不确定这是不是一个好习惯,但对我有用。

并确保我可以从刚添加的服务器获取数据:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

<script>
  $(document).ready(function() {
            $html = "";
            $.getJSON( "test.json", function( data ) {
               $.each(data.value.items,function(key,val) {
                 $html+='<li><h1>'+val.title+'</h1>';
                 $html+='<p>'+val.description+'</p></li>';
               });
               $('ul').appendTo($html);
            });
 });
    </script>