尝试使用underscore.js将名称从这个json中拉出来

时间:2012-12-28 17:17:29

标签: javascript json underscore.js

尝试使用underscore.js将该名称从这个json中拉出来,因为某些原因它无法通过结果。我用underscore.js模板取得了一些成功,但不是像这样的json。

<script type="text/html" id='usageList'>
    <table cellspacing='0' cellpadding='0' border='1'>
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
            </tr>
        </thead>
        <tbody>

            <% _.each(deals,function(item,key,list){ %>
                <% var f = item.Name; %><!-- create more variables -->
                <tr>
                    <!-- use variables -->
                    <td><%= key %></td>
                    <td class="<%= f %>"><%= item.name %> </td>
                </tr>
            <% }); %>
        </tbody>
    </table>
</script>

<div id="target"></div>

<script>

    var deals = { 
        "results" : [ 
            {
                "geometry" : {
                    "location" : {
                        "lat" : 35.2521950,
                        "lng" : -120.6917010
                    }
                },
                "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/shopping-71.png",
                "id" : "a60236447e99ae2b2fd0cde9b912f7abd91f4e1d",
                "name" : "Costco",
                "opening_hours" : {
                    "open_now" : true
                },
                "price_level" : 2,
                "reference" : "CqQBlgAAAJukesLD8HeY7_xb-   V51usJ1nVXJjiWVT3DRYVbParEaKTO8881ULz4f2RM8nGKVOHdIdM67oujeVWFA0YW63_ySzH8O9zTPPwT7h8MijD0BsKnue1BUb_eYHvIp4bCREitCI-b7LIJP_a4UzCrxzT8UsW28KYIParQKJJNfnDMXVvM2ymsvWiRGA42qre6Rhkn7DWkuGyG3zvgZ3xKLz1wSEGl0gCL7B5GNwBSRBytKgmwaFIh1TU7nJCMMsBj04tlA87HHh3Y3",
                "types" : [
                    "department_store",
                    "health",
                    "gas_station",
                    "grocery_or_supermarket",
                    "food",
                    "store",
                    "establishment"
                ],
            "vicinity" : "1540 Froom Ranch Way, San Luis Obispo"
            }
        ]
    };

    var template = $("#usageList").html();
    $("#target").html(_.template(template,{deals:results}));

</script>

1 个答案:

答案 0 :(得分:1)

认为您的模板调用不正确:

$("#target").html(_.template(template,{deals: deals.results}));

“results”值是“deals”全局引用的对象的属性,因此您需要以这种方式引用它。