weixin_33739541 2014-03-05 10:54 采纳率: 0%
浏览 21

JSON嵌套对象Web应用

So I'm trying to program a Web App (later hyprid app via Phonegap) which has Wordpress has backend (information will be dynamically load through json api)

But I'm stuck at the first step.

I want a menu where like this

  • Category 1
  • Category 2
  • Category 3

and the single categories link to the posts in the categorie

Here is a part of my json

{
"status": "ok",
"count": 10,
"count_total": 20,
"pages": 2,
"posts": [
    {
        "id": 86,
        "type": "post",
        "slug": "inaktiviert",
        "url": "http://localhost/wordpress/?p=86",
        "status": "publish",
        "title": "Inaktiviert",
        "title_plain": "Inaktiviert",
        "content": "<p>Das Feature ist inaktiviert.</p>
",
        "excerpt": "<p>Das Feature ist inaktiviert.</p>
",
        "date": "2014-03-04 15:09:51",
        "modified": "2014-03-04 15:09:51",
        "categories": [
            {
                "id": 6,
                "slug": "symbole-fuer-verschiedene-ereignisse",
                "title": "Symbole für verschiedene Ereignisse",
                "description": "",
                "parent": 0,
                "post_count": 4
            }
        ],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "admin",
            "name": "admin",
            "first_name": "",
            "last_name": "",
            "nickname": "admin",
            "url": "",
            "description": ""
        },
        "comments": [],
        "attachments": [],
        "comment_count": 0,
        "comment_status": "open",
        "custom_fields": {}
    },
    {
        "id": 84,
        "type": "post",
        "slug": "kann-nicht-aktualisiert-werden",
        "url": "http://localhost/wordpress/?p=84",
        "status": "publish",
        "title": "kann nicht aktualisiert werden",
        "title_plain": "kann nicht aktualisiert werden",
        "content": "<p>Das Feature kann nicht aktualisiert werden, der Fehler kann über das Fehlermanagement bzw. über Bearbeiten behoben werden.</p>
",
        "excerpt": "<p>Das Feature kann nicht aktualisiert werden, der Fehler kann über das Fehlermanagement bzw. über Bearbeiten behoben werden.</p>
",
        "date": "2014-03-04 15:09:25",
        "modified": "2014-03-04 15:09:25",
        "categories": [
            {
                "id": 6,
                "slug": "symbole-fuer-verschiedene-ereignisse",
                "title": "Symbole für verschiedene Ereignisse",
                "description": "",
                "parent": 0,
                "post_count": 4
            }
        ],
        "tags": [],
        "author": {
            "id": 1,
            "slug": "admin",
            "name": "admin",
            "first_name": "",
            "last_name": "",
            "nickname": "admin",
            "url": "",
            "description": ""
        },
        "comments": [],
        "attachments": [],
        "comment_count": 0,
        "comment_status": "open",
        "custom_fields": {}
    },


    {
        "id": 78,
        "type": "post",
        "slug": "das-braune-zahnrad",
        "url": "http://localhost/wordpress/?p=78",
        "status": "publish",
        "title": "Das braune Zahnrad",
        "title_plain": "Das braune Zahnrad",
        "content": "<p>Das braune Zahnrad und der rote Blitz zeigen an, dass die Teilereferenz kontextabhängig ist und dass dieses Exemplar nicht in der Teiledefinition verwendet wird. Dieses Kontextteil kann bearbeitet werden. Dieses Symbol wird möglicherweise angezeigt, wenn ein Kontextteil in ein anderes CATProduct kopiert/eingefügt wird, ohne dabei die Kon-textverknüpfungen zu berücksichtigen</p>
",
        "excerpt": "<p>Das braune Zahnrad und der rote Blitz zeigen an, dass die Teilereferenz kontextabhängig ist und dass dieses Exemplar nicht in der Teiledefinition verwendet wird. Dieses Kontextteil kann bearbeitet werden. Dieses Symbol wird möglicherweise angezeigt, wenn ein Kontextteil in ein anderes CATProduct kopiert/eingefügt wird, ohne dabei die Kon-textverknüpfungen zu berücksichtigen</p>
",
        "date": "2014-03-04 15:07:23",
        "modified": "2014-03-04 15:07:29",
        "categories": [
            {
                "id": 5,
                "slug": "symbole-fuer-kontextteile",
                "title": "Symbole für Kontextteile",
                "description": "",
                "parent": 0,
                "post_count": 3
            }
        ],

I tried to start programming but as I am a newbie in programming with ajax js I'm kinda lost

Here is my code I started writing, but it doesnt't show anything:

        <script>
        $(document).ready(function(){
            var liste = $('#liste');
            $.ajax({
                url: 'http://localhost/wordpress/?json=1',
                dataType: 'jsonp',
                success:
                 function(daten) {
                     var data = $.parseJSON(daten)
                    $.each(daten.categories, function(index, datensatz) {
                        $('<li><a href="#page' + datensatz.id + '">' + datensatz.title + '</a></li>').appendTo(liste);
                        $('<div data-role="page" data-add-back-btn="true" id="page' + datensatz.id + '"><div data-role="header"><h1>' + 
                        datensatz.title + '</h1></div><div data-role="content">' + datensatz.content + '</div></div>').appendTo('body');
                    });
                    liste.listview("refresh");
                }
            });
        });
    </script>
  • 写回答

1条回答 默认 最新

  • weixin_33739523 2014-03-05 14:36
    关注

    In your JSON, posts is an array of objects each of which contains a property called categories which is also an array of objects. So you $.each() should iterate over posts, and then within the each you need to iterate categories. You could do it like this:

    var liste = $('#liste');
    
    var AllListItems = '';
    var AllDynamicPages = '';
    $.each(daten.posts, function(index1, datensatz) {
        var postid = datensatz.id;
        var postTitle = datensatz.title;
        var postContent = datensatz.content;
    
        for (var i = 0; i< datensatz.categories.length; i++) {
            var catid = datensatz.categories[i].id;     
            var catTitle = datensatz.categories[i].title;            
            AllListItems += '<li><a href="#page' + catid + '">' + catTitle + '</a></li>';    
            AllDynamicPages += '<div data-role="page" data-add-back-btn="true" id="page' + catid + '"><div data-role="header"><h1>' + catTitle + '</h1></div><div data-role="content">' + postContent + '</div></div>'
        }        
    });
    
    liste.empty().append(AllListItems).listview("refresh");
    $("body").append(AllDynamicPages);
    

    DEMO

    NOTE: it is not clear from your JSON whether you really want the category ID which is not unique accross posts or the post id. So you will probably need to tweak the code to show the correct output...

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度