0

I have a set of data that is created/pulled from a url that has already got parsed json data like so:

[
{
"id": 1,
"school_name": "another school in essex",
"website": null,
"book_id": 27,
"url": "http://localhost:3000/schools/1.json"
},
{
"id": 2,
"school_name": "ddffdfd",
"website": null,
"book_id": 31,
"url": "http://localhost:3000/schools/2.json"
},
{
"id": 3,
"school_name": "ddfdfdffd",
"website": null,
"book_id": 31,
"url": "http://localhost:3000/schools/3.json"
},
{
"id": 4,
"school_name": "4545455454",
"website": null,
"book_id": 31,
"url": "http://localhost:3000/schools/4.json"
}
]

I have got a .each loop that grabs the data and assigns it to (city) parameter so I can grab that city parameter and loop through and just grab what I want from that json out put and use.

var info = '/schools.json';

$.get(info, function(response) {
  var jsonData = response
  var mapContent = $('').html();
  var json = jsonData;
}

function setMarkers(map, json) {
  jQuery.each(json, function (index, city) {
     console.log(city[id]);
   }
}

When I try and access the json object in the .each loop above it gives nothing. How can I access the id from each of the json outputs?

Cheers

2
  • You don't call setMarkers, so your each loop isn't going to do anything... Aside from that, you have some other weird things in your code. I'd suggest to clean it up first, to answer this would be to rewrite just about every line. Commented Sep 25, 2014 at 11:51
  • How do you call setMarkers? Commented Sep 25, 2014 at 11:51

3 Answers 3

1

Try this, you didn't called the setMarkers function, also, you didn't close the $.get function. Also when you get values from an object you should use obj['value'] or obj.value

var info = '/schools.json';

$.get(info, {}, function(response) {
  var mapContent = $('').html();
  setMarkers(mapContent, response);
}, 'json');

function setMarkers(map, json) {
  jQuery.each(json, function (index, city) {
     console.log(city['id']);
   }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent, that did the trick, I think my function was wrong but not completely wrong.
0
$.get(info,PARAMS, function(response) {
    var jsonData = response
    var mapContent = $('').html();
    var json = jsonData;
},"json");

I think you forgot the closing ');' and a function parameter: 'PARAMS' that can be null Just try adding "json" as final argument.

Hope it helps

Comments

0

your code should return ReferenceEerror: id is not defined. use city["id"] or city.id instead.

And check that json is valid json object, not string

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.