0

I wonder if you could help me out. I've been trying to get JSON to work with Angular.js but I've been having a rough time with the specifics of a JSON $http.get() call. I would like to simply just have each person's name show up under the 'people' heading on the webpage but my JSON call doesn't want to work. I know it's probably a stupid small syntax error but I can't seem to find it. All help appreciated. Here is my code:

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ng-app='People'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-rc.4/angular.min.js"></script>
<script type="text/javascript" src="app.js"></script>
<title>People Webpage</title>
</head>

<body>
<h3>People</h3>
<div ng-controller='PeopleController as peeps'>
<div ng-repeat='person in peeps.people'>
<h5>{{person.name}}</h5>
</div>
</div>
</body>
</html>

JSON:

{
{ 'name' : 'Jack', 'age' : '28', 'weight' : '75kg'},
{ 'name' : 'Jill', 'age' : '25', 'weight' : '66kg'}
}

JAVASCRIPT:

var app = angular.module('People',[]);

app.controller('PeopleController',[ '$http' ,function($http){

    var names = this;

    names.people = [];

    $http.get('people.json').success(function(data){

        names.people = data;


    });

}]);

I tried alert(data); in the success callback and I'm receiving the JSON as this:

"{{ 'name' : 'Jack', 'age' : '28', 'weight' : '75kg'},{ 'name' : 'Jill', 'age' : '25', 'weight' : '66kg'}}"

However, if try to call an alert on a JavaScript object created like so:

 var people = [
{ 'name' : 'Jack', 'age' : '28', 'weight' : '75kg'},
{ 'name' : 'Jill', 'age' : '25', 'weight' : '66kg'}
];

alert(people);

I receive something to the effect of :

{{ object : Object,  object : Object,  object : Object},
{ object : Object, object : Object, object : Object}}

Is this relevant to my issue?

9
  • 6
    Is that JSON accurate? Looks like the outer curly braces are probably supposed to be square brackets (array)... Commented Oct 7, 2014 at 19:33
  • Agreed you need the JSON to output an array for person in peeps.people to work Commented Oct 7, 2014 at 19:36
  • How would I get the JSON to output an array? Commented Oct 7, 2014 at 19:42
  • With [] brackets around the JSON as opposed to {} ? Commented Oct 7, 2014 at 19:44
  • 1
    Yes use [] instead of {} for the first set of braces. Do you have any control over how the JSON is outputted? Commented Oct 7, 2014 at 19:44

3 Answers 3

2

How your JSON should be:

{
"people": [
    {
        "name": "Jack",
        "age": "28",
        "weight": "75kg"
    },
    {
        "name": "Jill",
        "age": "25",
        "weight": "66kg"
    }
]
}

How your call should be:

$http.get('people.json').success(function(data){

    names.people = data.people;


});

The method you're using with $http.get for the success is something I don't typically do, so you're either looking at data.people or data.data.people depending on the response you received from the call.

console.dir(data) 

Would give you a good look at the response being received.

Sign up to request clarification or add additional context in comments.

3 Comments

Tried what but never worked? You need to be more descriptive of what you're seeing/error receiving/ etc.
I got this response in the console: GET (hostingaddress) net::ERR_BLOCKED_BY_CLIENT weburl/:21
Thank you, you were technically the best answer as you wrote it in with double quotes " ". Sorry for doubting you. I tested it your answer with single quotes.
1

Ok was hoping someone else would write this but:

Use this as your JSON

[
    { 'name' : 'Jack', 'age' : '28', 'weight' : '75kg'},
    { 'name' : 'Jill', 'age' : '25', 'weight' : '66kg'}
]

5 Comments

I was about to. First thing I noticed was the invalid JSON -- for future use: jsonlint.com
Well if he doesn't have control over how the JSON is outputted he has a different problem althogether.
Yep, just saw that too.
Thanks for the advice but it still doesn't work and if I call an alert(data) on the JSON it fails to even give me a response so I assume the $http never succeeds with the [ ] braces. Any other ideas?
Its always good practice to validate JSON object before using it in application or finalising it for any API. 1. jsonlint.com 2. jsonapi.org
0

After playing around with my JSON I realized what the error was, thank you everyone for the help.

[
    {
        "name": "Jack",
        "age": "28",
        "weight": "75kg"
    },
    {
        "name": "Jill",
        "age": "25",
        "weight": "66kg"
    }
]

The issue was that I first of all used { } as an outer encapsulation instead of [ ] , thank you to everyone who suggested that. Moreover, JSON requires double quotes " " as opposed to single quotes ' '. Thank you especially to Likwid_T for chatting with me and attempting to help, I really appreciate it.

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.