2012-01-21 21 views
1

認識できない表現

[ 
{ 
    "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でこのエラーメッセージが表示される理由についてのご意見はありませんか。

Uncaught Error: Syntax error, unrecognized expression: #[object Object]

答えて

1

jsonオブジェクトはオブジェクトの配列です。 data [i]を渡すと、配列の最初の要素、オブジェクト、および文字列(containerID)のように扱われます。渡すオブジェクトからIDを取得する必要があります。

1

dataは、投稿の冒頭にjson文字列が記載されているとします。 dataはオブジェクトの配列です。だから

for(var i=0; i<data.length; i++){ divifyCategory(data[i]); }

オブジェクトは、最初のパラメータとして文字列を期待しdivifyCategory機能に渡されました。そして、そのパラメータを使用すると、$('#[object Object]')を取得し、実行時に文字列が

$('#' + containerID)

のように使用されます。それは予期しない状況です。

私はそれが役に立ちそうです。

関連する問題