How do I properly format a JSON object in an external file?

admin

Administrator
Staff member
I have used the code available from <a href="http://codeblitz.wordpress.com/2009/06/22/jquery-charts/" rel="nofollow">http://codeblitz.wordpress.com/2009/06/22/jquery-charts/</a>

It uses jqPlot. So I have the following sample code Default.html that works:

Code:
&lt;script type="text/javascript"&gt;

var jsonObj = { "pageHits": [30, 60, 22, 5, 60, 88, 102], "rssHits": [33, 45, 121, 23, 55, 35, 77], "xAxis": ['Jan 2009', 'Feb 2009', 'Mar 2009', 'Apr 2009', 'May 2009', 'June 2009', 'Jul 2009'] };

$(function () {
    $.jqplot('chartDiv', [jsonObj.pageHits, jsonObj.rssHits], CreateBarChartOptions());
});

    function CreateBarChartOptions()
    {
        var optionsObj = {
            title: 'Blog Statistics',
            axes: {
                 xaxis: {
                    renderer: $.jqplot.CategoryAxisRenderer,
                    ticks: jsonObj.xAxis
                }
            },
            series: [{label:'Page Hits'}, {label: 'RSS Hits'}],
            legend: {
                show: true,
                location: 'nw'
            },
            seriesDefaults:{
                shadow: true,
                renderer:$.jqplot.BarRenderer,
                rendererOptions:{
                   barPadding: 8,
                   barMargin: 10
               }
            },
            highlighter: {
                showTooltip: true,
                tooltipFade: true
            }
        };
        return optionsObj;
    }
&lt;/script&gt;

I have copied the code and put it into Default.aspx. The only thing I want to change is to be able to get the data from an external file, so now my code is:

Code:
&lt;script type="text/javascript"&gt;

  var jsonObj;
  $.getJSON('example.json', function (response)
  {
    jsonObj = response;
    alert(jsonObj.property);
  });

$(function () {
  $.jqplot('chartDiv', [jsonObj.pageHits, jsonObj.rssHits], CreateBarChartOptions());
    });

    function CreateBarChartOptions()
    {
      var optionsObj = {
        title: 'Blog Statistics',
        axes: {
          xaxis: {
            renderer: $.jqplot.CategoryAxisRenderer,
            ticks: jsonObj.xAxis
          }
        },
        series: [{ label: 'Page Hits' }, { label: 'RSS Hits'}],
        legend: {
          show: true,
          location: 'nw'
        },
        seriesDefaults: {
          shadow: true,
          renderer: $.jqplot.BarRenderer,
          rendererOptions: {
            barPadding: 8,
            barMargin: 10
          }
        },
        highlighter: {
          showTooltip: true,
          tooltipFade: true
        }
      };
      return optionsObj;
    }

&lt;/script&gt;

But jsonObj is always undefined, I'm presuming my file is not formatted properly. I have tried example.json to contain this:

{"pageHits": [30, 60, 22, 5, 60, 88, 102], "rssHits": [33, 45, 121, 23, 55, 35, 77], "xAxis": ['Jan 2009', 'Feb 2009', 'Mar 2009', 'Apr 2009', 'May 2009', 'June 2009', 'Jul 2009']}

and this:

{"pageHits": [30, 60, 22, 5, 60, 88, 102], "rssHits": [33, 45, 121, 23, 55, 35, 77], "xAxis": ["Jan 2009", "Feb 2009", "Mar 2009", "Apr 2009", "May 2009", "June 2009", "Jul 2009"]}

But to no avail. What am I doing wrong?

Thanks for any assistance,

Julian