1

I am new to javascript and I am trying to parse and manipulate the data below but not too sure how to go about it. Can anyone help please?

JSON Data

{
    "2009-01": {
        "bbcfour": 324,
        "bbcnews24": 1075,
        "bbcone": 940,
        "bbcthree": 441,
        "bbctwo": 1040,
        "cbbc": 898,
        "cbeebies": 1343
    },
    "2009-02": {
        "bbcfour": 295,
        "bbcnews24": 958,
        "bbcone": 904,
        "bbcthree": 434,
        "bbctwo": 1038,
        "cbbc": 793,
        "cbeebies": 1246
    }}

JavaScript

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="content-language" content="en" />
<meta http-equiv="Content-Type" content="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" />
<link href="main.css" media="all" rel="stylesheet" type="text/css" />
</head>
<body>
<script src="jquery-2.1.1.js"></script>
<script>
$.getJSON('data.json', function (data) {
    console.log(data);
});
</script>
</body>
</html>
3
  • You don't have to parse it; jQuery will do that implicitly. What is it that you want to do with the data that you're fetching? Commented Feb 21, 2015 at 17:44
  • json is a dictionary structure , means key,value. if you want to read any value use. jsonobject.key Commented Feb 21, 2015 at 17:45
  • I want to be able to take each object e.g. 2009-01 and print its content Commented Feb 21, 2015 at 17:48

1 Answer 1

4

Use for...in loop.

The for..in statement iterates over the enumerable properties of an object, in arbitrary order.

   $.getJSON('data.json', function (data) {
        var resp = data;
        for(k in resp)
        {
          console.log(resp[k].bbcfour);
          console.log(resp[k].bbcnews24);
          console.log(resp[k].bbcone);
          // And so on
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

thank you but i get an 'undefined object' as a result
should be more like console.log(data[k].bbcfour);
Thanks so much guys, appreciate the help as Im new to this. Thank you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.