1

I have an external JSON file hotel.json.

{
    "hotel_info": [{
        "booking_id": "2",
        "hotel_name": "Ascot Lodging",
        "star_id": "2",
        "street": "Via Vincenzo da Seregno",
        "province": "Milan",
        "price_per_night": "417.00"
    }]
}

I'm using AngularJS to read this and display the data. In my controller I have:

app.controller('mainController', function($scope,$http) {
    $http.get('includes/hotel.json').success(function (data) {
    $scope.hotel = data;
   });
});

I should then be able to display the data on the HTML page using:

<h3>{{ hotel.hotel_info.hotel_name }}</h3>

This should display "Ascot Lodging" but it doesn't display anything.

The data is being passed through because if I remove .hotel_name from the handlebars it simply returns the whole JSON text.

Does anyone know why I can't access the individual information?

Thank you in advance.

1
  • MrCode is right. If you json file has info about only one hotel i wouldnt put it in an array and instead have it as an object map: { "booking_id": "2", "hotel_name": "Ascot Lodging", "star_id": "2", "street": "Via Vincenzo da Seregno", "province": "Milan", "price_per_night": "417.00" } This is essentially all you need and the interpolation would look like {{hotel.hotel_name}} Commented Apr 18, 2014 at 14:39

1 Answer 1

1

In your JSON hotel_info is an array not an object (the square brackets [] are the array notation). Change to:

<h3>{{ hotel.hotel_info[0].hotel_name }}</h3>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, something so simple!

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.