我从Web服务获得以下json输出:

[
{
    "subCategories": [
    {
        "subCategories": [],
        "menuItems": [],
        "id": 2,
        "title": "First Course",
        "type": "Menu Section",
        "categoryID": 9,
        "isActive": true,
        "orderIndex": 7
    }, {
        "subCategories": [],
        "menuItems": [
        {
            "id": 0,
            "price": 30,
            "title": "Meat",
            "ingredients": "Bread, Pate, Cilantro, Turkey.",
            "cookingTimeInMinutes": 6,
            "isActive": true,
            "picture": "",
            "categoryID": 3,
            "orderIndex": 2
        }],
        "id": 3,
        "title": "Banh Mi",
        "type": "Food Item",
        "categoryID": 9,
        "isActive": true,
        "orderIndex": 1
    }],
    "menuItems": [
    {
        "id": 1,
        "price": 1,
        "title": "Soup",
        "ingredients": "Water, Good Stuffs, Noodles.",
        "cookingTimeInMinutes": 10,
        "isActive": true,
        "picture": "",
        "categoryID": 9,
        "orderIndex": 4
    }, {
        "id": 3,
        "price": 12,
        "title": "Egg Sandwich",
        "ingredients": "Egg, Sandwich",
        "cookingTimeInMinutes": 6,
        "isActive": true,
        "picture": "",
        "categoryID": 9,
        "orderIndex": 3
    }],
    "id": 9,
    "title": "Lunch",
    "type": "Menu Section",
    "categoryID": null,
    "isActive": true,
    "orderIndex": 0
}, {
    "subCategories": [],
    "menuItems": [],
    "id": 7,
    "title": "Snack",
    "type": "Menu Section",
    "categoryID": null,
    "isActive": true,
    "orderIndex": 8
}, {
    "subCategories": [],
    "menuItems": [],
    "id": 6,
    "title": "First Course",
    "type": "Menu Section",
    "categoryID": null,
    "isActive": true,
    "orderIndex": 5
}, {
    "subCategories": [],
    "menuItems": [
    {
        "id": 2,
        "price": 3,
        "title": "Salad",
        "ingredients": "Veggies",
        "cookingTimeInMinutes": 5,
        "isActive": true,
        "picture": "",
        "categoryID": null,
        "orderIndex": 9
    }],
    "id": -1,
    "title": "Other",
    "type": "Menu Section",
    "categoryID": null,
    "isActive": true,
    "orderIndex": 1000
}]


而且我有以下javascript代码段,该代码段应遍历提到的json并将其转换为div:

<script type="text/javascript">
    /* wait until the document has finished loading before loading
     * the rest of the content
     */
    $(document).ready(function(){
        function divifyCategory(containerID, gingerWebCategory){
            $('#' + containerID).append(
                $('<div class="category" id="' + gingerWebCategory.id + '">' + gingerWebCategory.title + '</div>')
            );
            for(menuItem in gingerWebCategory.menuItems){
                $('.category#' + gingerWebCategory.id).append(
                    $('<div class="menuItem" id="' + menuItem.id + '">' + menuItem.title + '</div>')
                );
            }
        }

        // load menu from web service
        $.get('http://localhost:50730/GingerWeb.asmx/getMenu', function(data){
            var data = eval(data);
            for(var i=0; i<data.length; i++){
                divifyCategory(data[i]);
            }
        });
    });
</script>


关于我为什么在Chrome中收到此错误消息的任何想法:


  未捕获的错误:语法错误,无法识别的表达式:#[object
  宾语]


最佳答案

您的json对象是对象数组。传递data [i]时,将传递数组中的第一个元素,一个对象,并将其视为字符串(containerID)。您需要从要传递的对象中获取ID。

关于javascript - 无法识别的表情,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8952718/

10-13 07:16