如何解析json对象以使用主干获取页面中的数据

如何解析json对象以使用主干获取页面中的数据,json,object,backbone.js,Json,Object,Backbone.js,我用的是主干线。 我有一个json数据,如: {"status": 7, "organizations":[{"org_address":"\u4e2d\u56fd\u5317\u4eac\u5e02\u671d\u9633\u95e8\u59161001\u53f7", "job_positions": ["\u526f\u603b"], "org_id": 6, "org_name": "\u6570\u7ef4\u7f51\u7edc"}], "first_name": null, "

我用的是主干线。 我有一个json数据,如:

{"status": 7, 
 "organizations":[{"org_address":"\u4e2d\u56fd\u5317\u4eac\u5e02\u671d\u9633\u95e8\u59161001\u53f7", "job_positions": ["\u526f\u603b"], "org_id": 6, "org_name": "\u6570\u7ef4\u7f51\u7edc"}], "first_name": null, "last_name": null, "create_date": "2013-06-26 16:29:44.314000", "name": "\u66f9\u4f1f", "extra_emails": [null], "tags": [["friend"]], "nick_name": "\u4f11\u606f\u4e00\u4f1a\u513f", "gender": "\u7537", "image": null, "created_by": "system", "effective_start_date": "2013-06-26 16:29:44.314000", "social_medias": [{"url": "http://weibo.com/12345", "party_rel_id": 5, "type": "\u65b0\u6d6a\u5fae\u535a", "party_id": null, "sm_id": "\u6570\u636e\u5e93\u4e13\u5bb6"}], "date_of_birth": "1980-04-01", "extra_phones": {"office_phone": "82323333", "fax": "82324433"}, "mobile_phone": "17782272211", "primary_email": "weic@xy.com", "id": "5", "isUser": false}

我的主干模型是:

var ContactDetail = Backbone.Model.extend({  
    urlRoot: URL_CONTACTS1+"3/5"
});

var ContactDetailCollection =  Backbone.Collection.extend({ 
    model: ContactDetail,
    url: URL_CONTACTS1+"3/5"
})
我的主要观点是:

var ContactDetailItemView = Backbone.View.extend({
    tagName: 'div', 
    tagClass: 'border',
    template: _.template($('#contactdetail-template').html()),  
        render: function() {
        this.$el.html( this.template(this.model.toJSON()));
        return this;
    }
});
var ContactDetailListView =  Backbone.View.extend({
    el: $('#main'),
    initialize:  function(){
        this.listenTo(this.collection, 'reset', this.render);
    },  

        render: function() {
            this.$el.empty(); 
            this.collection.each( function( $model ) {
            var itemview = new ContactDetailItemView({model: $model}); 
        this.$el.append( itemview.render().el );  
            }, this);
        return this;
    },
})
入口为:

ContactDetailManagePageModel.prototype.init = function(){ 
    var myContactDetails = new ContactDetailCollection();
    contactDetailListView = new ContactDetailListView({
            collection: myContactDetails
        });     
    myContactDetails.fetch({reset:true});
}
在html中,我得到如下数据:

<tr>
   <td><span class="Name_text"><%=name%></span>&nbsp;&nbsp;&nbsp;&nbsp;<%=primary_email%></td>
</tr>
<tr>
    <td><%=mobile_phone%></td>
 </tr>
 <tr>
    <td><%=org_address%></td>
 </tr>

但我无法获取json数据中对象“organizations”中名为“org\u address”的数据,它表示“org\u address”未定义


希望你的帮助。谢谢

尝试

当您将数据对象传递给下划线模板时,下划线会将对象属性添加到其自由变量中,如果其中一个属性反过来是数组或本身是对象,则您可以使用点或括号表示法访问它,就像在常规JavaScript中一样

例如,对于JSON,因为organizations是一个数组,它的第一个元素包含属性org\u address,所以您只需执行以下操作即可获得org\u address

<%=organizations[0].org_address%>

您还可以在模板中执行JavaScript代码,因此,例如,如果组织包含多个组织的数组,您可以循环遍历它们并将它们全部打印出来。比如说

<%  for (var i = 0, len = organizations.length; i < len; i++) { %>
     <tr><td>organizations[i].org_address</td></tr>
<% } %>

组织[i].组织地址
您可以执行以下操作