JSON文件未加载

时间:2014-08-24 13:45:19

标签: jquery json

我正在尝试加载一个json文件,但它不会加载并且它一直发出一个奇怪的错误:" TypeError:e未定义"。

我不确定$.each(data.Dishes) is correct or is it $.each(data.Dishes.Lunch) 这就是我现在在剧本中所拥有的:

$(document).ready(function() {
    $(".lunch_section").empty();
    $(".diner_section").empty();

    $.ajax({
        url: 'Menu.json',
        dataType: 'json',
        success: function(data) {
            $.each(data.Dishes, function(index) {
                $('section.lunch_section').append("<h2 class='sectiontitel'> <span> Lunch </span> </h2><article class='Lunch_article'><img src='" + data.Dishes.Lunch[index].Image + "' alt='food_image'/>");
            });
        },

        // eventuele foutafhandeling....
        error: function() {
            alert("Houston, we have a problem");
        }
    });
});

这就是我的json的样子:

[
    {
        "Dishes":
        [
            {
                "Lunch": 
                [
                    {
                        "Name":"Bento Box",
                        "type":"main",
                        "Image":"Images/lunch/Bento.jpg",
                        "description":"Bento is a single-portion takeout or home-packed meal common in Japanese cuisine. A traditional bento holds rice, fish or meat, with pickled or cooked vegetables, usually in a box-shaped container. Containers range from disposable mass produced to hand crafted lacquerware. Bento boxes are readily available in many places throughout Japan, including convenience stores, bento shops, railway stations, and department stores. However, Japanese homemakers often spend time and energy on a carefully prepared lunch box for their spouse, child, or themselves.",
                        "url":"http://en.wikipedia.org/wiki/Bento",
                        "ingredients": 
                                [
                                    {
                                        "no1":"Rice",
                                        "no2":"Fish",
                                        "no3":"Seaweed",
                                        "no4":"Soy Sause",
                                        "no5":"vegetables",
                                        "no6":"meat"
                                    }
                                ]
                    },
                    {
                        "Name":"Miso Soup",
                        "type":"entree",
                        "Image":"Images/lunch/miso.jpg",
                        "description":"Miso soup (misoshiru?) is a traditional Japanese soup consisting of a stock called \"dashi\" into which softened miso paste is mixed. Many ingredients are added depending on regional and seasonal recipes, and personal preference.",
                        "url":"http://en.wikipedia.org/wiki/Miso_soup",
                        "ingredients": 
                                [
                                    {
                                        "no1":"Dashi",
                                        "no2":"Tofu",
                                        "no3":"Miso Paste"
                                    }
                                ]
                    }

                ],


                "Diner": 
                    [
                        {
                            "Name":"Sake Nigiri",
                            "type":"Nigiri",
                            "Image":"Images/diner/sake_nigiri.jpg",
                            "description":"Nigirizushi (\"hand-pressed sushi\") consists of an oblong mound of sushi rice that the chef presses into a small rectangular box between the palms of the hands, usually with a bit of wasabi, and a topping (the neta) draped over it. Neta are typically fish such as salmon, tuna or other seafood. Certain toppings are typically bound to the rice with a thin strip of nori, most commonly octopus (tako), freshwater eel (unagi), sea eel (anago), squid (ika), and sweet egg (tamago). One order of a given type of fish typically results in two pieces, while a sushi set (sampler dish) may contain only one piece of each topping",
                            "url":"http://en.wikipedia.org/wiki/Sushi",
                            "ingredients": 
                                    [
                                        {
                                            "no1":"Rice",
                                            "no2":"Salmon",
                                            "no3":"Wasabi paste",
                                            "no4":"Soy Sause"
                                        }
                                    ]
                        },

                        {
                            "Name":"Sake Maki",
                            "type":"Maki",
                            "Image":"Images/diner/Sake_maki.jpg",
                            "description":"Makizushi (\"rolled sushi\"), norimaki (\"Nori roll\") or makimono (\"variety of rolls\") is a cylindrical piece, formed with the help of a bamboo mat known as a makisu. Makizushi is generally wrapped in nori (seaweed), but is occasionally wrapped in a thin omelette, soy paper, cucumber, or shiso (perilla) leaves. Makizushi is usually cut into six or eight pieces, which constitutes a single roll order. Below are some common types of makizushi, but many other kinds exist.",
                            "url":"http://en.wikipedia.org/wiki/Sushi",
                            "ingredients": 
                                    [
                                        {
                                            "no1":"Rice",
                                            "no2":"Seaweed",
                                            "no3":"Salmon"
                                        }
                                    ]
                        },

                        {
                            "Name":"Sake Temaki",
                            "type":"Temaki",
                            "Image":"Images/diner/sake_temaki.jpg",
                            "description":"Temaki (\"hand roll\") is a large cone-shaped piece of nori on the outside and the ingredients spilling out the wide end. A typical temaki is about ten centimeters (4 in) long, and is eaten with fingers because it is too awkward to pick it up with chopsticks. For optimal taste and texture, temaki must be eaten quickly after being made because the nori cone soon absorbs moisture from the filling and loses its crispness, making it somewhat difficult to bite through. For this reason, the nori in pre-made or take-out temaki is sealed in plastic film which is removed immediately before eating.",
                            "url":"http://en.wikipedia.org/wiki/Sushi",
                            "ingredients": 
                                    [
                                        {
                                            "no1":"Rice",
                                            "no2":"Salmon",
                                            "no3":"Wasabi paste",
                                            "no4":"Avocado",
                                            "no4":"Seaweed"
                                        }
                                    ]
                        } 

                    ] 
                }
            ]
        }
    ] 

编辑:

$.ajax(
            {
                url:        'Menu.json',
                dataType:   'json',
                success:    function(data)
                {                       
                    $.each(data[0].Dishes, function(index)
                    {
                        $('section.lunch_section').append("<h2 class='sectiontitel'> <span> Lunch </span> </h2><article class='Lunch_article'><img src='" 
                            + data[0].Dishes[0].Image
                            + "' alt='food_image'>");
                    });
                },

                // eventuele foutafhandeling....
                error: function()
                {
                    alert("Houston, we have a problem");
                }   
}); 

2 个答案:

答案 0 :(得分:0)

您的JSON使用一个条目定义数组,该条目是具有一个属性Dishes的对象,其值为数组。所以:

// Get first array entry
//         vvv
$.each(data[0].Dishes, function...

答案 1 :(得分:-1)

在尝试使用$ .each

进行迭代之前尝试使用$ .parseJson(数据)