S
S
Sergey Pavlov2014-07-14 21:45:05
JavaScript
Sergey Pavlov, 2014-07-14 21:45:05

How to get data from json using jquery?

data.json:
{ 'new':'asdsa',
'list':[{
'name':'first',
'rating':'50%',
'story':'Once upon a time'
},
{
' name':'second',
'rating':'65%',
'story':'New chapter'
}]
}
Script:

<script type="text/javascript" src="jquery-1.10.2.min.js"></script>
        
        <script type="text/javascript">
        $.getJSON('data.json', function(data){
            alert('asdas');
            alert(data.list[1].name);
        });
        </script>


The output is expected to receive "second", but there is nothing. Where is the error in this code, or how can it be written differently, perhaps in native JS, without using Jquery?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Melnikov, 2014-07-15
@Pavlov_dog

Learn how to code for the fastest response!
JSON

{
  "new":"asdsa",
  "list":
    [
      {
        "name":"first",
        "rating":"50%",
        "story":"Once upon a time"
      },
      {
        "name":"second",
        "rating":"65%",
        "story":"New chapter"
      }
    ]
}

JS
$.getJSON('data.json', function(data){
  console.log(data);
});

look at the console output for any errors.

P
Pavel Shvedov, 2014-07-14
@mmmaaak

If you try to make such a request without using a web server (by opening the page as a file at "file:///...", locally) then the request will fail. Are you using a web server?

N
Nazar Mokrinsky, 2014-07-15
@nazarpc

Write
See what comes.
And yes, your JSON is not valid.

W
WarGot, 2014-07-15
@WarGot

Check first json through e.g. json.parser.online.fr for validity

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question