0

My Json array is as follow how to extract Name and temp using 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"
        }
      ]
    },

My JavaScript is as follows:

<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>

How to get Name and temperature from above Json , they are in different Json object inside Json object

1
  • Do you need other data attributes aswell? Do attributes change during the lifetime of the application? Will you need event listeners? Do you need to save the model to the server? Commented Oct 17, 2015 at 0:03

1 Answer 1

1

by

How to get Name and temperature from above Json

if you meant to access 'name' and 'temp', you can access them as follows once they are set in a model.

model.get("name");  //"Shcherbinka"
model.get("main").temp; //274.72
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.