Json上传时出现jquery文件错误

时间:2014-05-14 23:28:37

标签: javascript jquery ajax json

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <script src="jquery-1.11.0.js"></script>
    <script type="text/javascript">
    function restults(data) 
    {
        $.each(data, function(index, data) 
        {
            alert(data.menu);
            $("#menu").append("<li>MenuID: " + data.MenuID + " OmfID: " + data.OmfID + " menu_name: " + data.menu_name + "</li>");
        });

    }
    $(document).ready(function()
    {
        setInterval(function() 
        {
            $.ajax(
            {
                type: "GET",
                dataType: "json",
                url: "json.json",
                success: function(data)
                {
                   restults(data);
                }
            });
        }, 1000);
    });
    </script>
    </head>
    <body>
    <div id="menu">
    </div>
    </body>
    </html>

这是我的json文件

[
{"menu":{"MenuID":"Test 1302662957","OmfID":"http:\/\/www.google.com","menu_name":"Zuker"}},
{"menu":{"MenuID":"Test 1302662957","OmfID":"http:\/\/www.google.com","menu_name":"Zuker"}},
{"menu":{"MenuID":"Test 1302662957","OmfID":"http:\/\/www.google.com","menu_name":"Zuker"}},
{"menu":{"MenuID":"Test 1302662957","OmfID":"http:\/\/www.google.com","menu_name":"Zuker"}},
{"menu":{"MenuID":"Test 1302662957","OmfID":"http:\/\/www.google.com","menu_name":"Zuker"}}
]

我的javascript设置现在运行时没有{“menu”:在json文件中,但我必须在json文件中有{“menu”:我的问题是:

  1. 如何用{“menu”:{在文件中??
  2. 循环遍历整个json文件

1 个答案:

答案 0 :(得分:0)

只是对迭代方式进行一些小改动

function restults(result) {
    $.each(result, function(index, data) {
        var menu = data.menu;
        var li   = $('<li />', {
            text : "MenuID: " + menu.MenuID + " OmfID: " + 
                    menu.OmfID + " menu_name: " + menu.menu_name
        });

        $("#menu").append(li);
    });
}