javascript - Backbone 从嵌套 json 获取数据

标签 javascript json backbone.js nested

我的 Json 数组如下如何使用 Backbone 提取名称和临时

{
  "message": "accurate",
  "cod": "200",
  "count": 10,
  "list": [
    {
      "id": 495260,
      "name": "Shcherbinka",
      "coord": {
        "lon": 37.559719,
        "lat": 55.499722
      },
      "main": {
        "temp": 274.72,
        "pressure": 1033,
        "humidity": 93,
        "temp_min": 272.04,
        "temp_max": 277.04
      },
      "dt": 1444968405,
      "wind": {
        "speed": 2,
        "deg": 300
      },
      "sys": {
        "country": ""
      },
      "clouds": {
        "all": 76
      },
      "weather": [
        {
          "id": 803,
          "main": "Clouds",
          "description": "broken clouds",
          "icon": "04d"
        }
      ]
    },

我的 JavaScript 如下:

<script>


      $(function() {
                var Profile = Backbone.Model.extend();

                var ProfileList = Backbone.Collection.extend({
                    model: Profile,
                    url: "http://api.openweathermap.org/data/2.5/find?lat=55.5&lon=37.5&cnt=10&appid=bd82977b86bf27fb59a04b61b657fb6f",     
                    parse : function(response){
                    weather_data = response.list
                    console.log(weather_data)
                        return weather_data;  
                   }   
                });   

                var ProfileView = Backbone.View.extend({
                    el: "#profiles",
                    template: _.template($('#profileTemplate').html()),
                    render: function(eventName) {
                        _.each(this.model.models, function(profile){
                            var profileTemplate = this.template(profile.toJSON());
                            $(this.el).append(profileTemplate);
                        }, this);

                        return this;
                    }
                });

                var profiles = new ProfileList();  
                profiles.fetch();
                var profilesView = new ProfileView({model: profiles});
                profiles.fetch({
                    success: function() {
                        profilesView.render();
                    }
                });

            });
        </script>

如何从上面的 Json 中获取 NameTemperature ,它们位于 Json 对象内的不同 Json 对象中

最佳答案

How to get Name and temperature from above Json

如果您打算访问“name”和“temp”,则在模型中设置它们后,可以按如下方式访问它们。

model.get("name");  //"Shcherbinka"
model.get("main").temp; //274.72

关于javascript - Backbone 从嵌套 json 获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33162902/

相关文章:

javascript - Backbone.js 路由条件默认路由

javascript - 发送后无法设置 header 。 (nodejs错误)

javascript - .animate() slider 无法正常工作

javascript - 是否可以在可见性隐藏元素上设置 slideDown 动画?

javascript - 带空格的可动态 json 数据

javascript - 在用数据填充 json 文件之前等待所有图像加载的正确方法

javascript - 主干路由器工作不正常

javascript - 使用 jQuery 交换选择列表中的值

javascript - Jquery 自动完成的 JSON

django respite + backbone 在获取 url 时搞砸了