0

I want to get data out of a JSON file and show it. I am trying to do this with jQuery...

$(document).ready(function () {
   var url = 'http://domain.com/test/json.json';
   $.getJSON(url, function(data){
      for (var i = 0; i < data.length; i++) {
         if (json[i].Active === "True") {
            $('<li />', {text : data[i].Name}).appendTo('.feed');
         }
      };
   });
});

JSON file looks like this:

 {"List":[
        {"Name":"Name 1","Active":"True"},
        {"Name":"Name 2","Active":"False"},
        {"Name":"Name 3","Active":"True"},
        {"Name":"Name 4","Active":"False"},
        {"Name":"Name 5","Active":"True"},
        {"Name":"Name 6","Active":"True"},
        {"Name":"Name 7","Active":"False"},
        {"Name":"Name 8","Active":"True"},
        {"Name":"Name 9","Active":"False"},
        {"Name":"Name 10","Active":"True"}
    ]}

HTML should look like this:

<div class="feed">
   <li>Name 1</li>
   <li>Name 3</li>
   <li>Name 6</li>
   <li>Name 8</li>
   <li>Name 10</li>
   //etc...
</div>
6
  • 2
    Where is your question? What are your errors? And besides that: The JSON response is inside the "data" object - you are not accessing it at all. Commented Jul 28, 2014 at 13:22
  • @gulty nothing is showing up... it's blank. Commented Jul 28, 2014 at 13:28
  • change json to data inside your getJSON function - if it's still not working you have to start debugging your code using console.log(data.Name) for instance. Commented Jul 28, 2014 at 13:32
  • 2
    looks like you are trying to access the json file from a different domain which isn't allowed due to the same origin policy: de.wikipedia.org/wiki/Same-Origin-Policy you have to change the htaccess file to allow your request on the destination server or use jsonP Commented Jul 28, 2014 at 13:38
  • it is not defined because your response is empty. you can't access the .json file from a different domain. Commented Jul 28, 2014 at 13:48

1 Answer 1

4
$(document).ready(function () {
   var url = 'http://domain.com/test/json.json';
   $.getJSON(url, function(data){
      for (var i = 0; i < data.List.length; i++) {
         var item = data.List[i];
         if (item.Active === "True") {
            $('<li />', {text : item.Name}).appendTo('.feed');
         }
      };
   });
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.