0

I have been surfing SO for quite some time to find an answer to my question, and I have to admit that I am stumped. I think I must be missing something very simple here but right now I can't see the wood for the trees.

I am trying to load a highchart via JSON, and the JSON is created by a PHP array (the reason for this is that I am retrieving the data from MySQL.) The series are loading fine, however I cannot extract the title for the chart. My understanding is that the code for that needs to be something like json.title.text ... however this does not work and crashes the script. Any help would be greatly appreciated!

The PHP array is as follows (I have put dummy variable to simplify...):

    $arr = 
array (
    array (
        'title' => array (
            'text' => 'idiot'
        ),
        'data' => array (
            '2012-12-16; 0',
            '2012-12-16; 23'
        )
    ),
    array (
        'name' => 'Sacred cows',
        'data' => array (
            98.9914,
            99.5429
        )
    ),

);
echo json_encode($arr);

The javascript that generates the charts is as follows:

  function marketwidget(id){

var formData = "name="+ id + "&age=31";

$.ajax({
    url : "marketwidget.php",
    type: "POST",
    data: formData,
    success: function(data, textStatus, jqXHR)
    {
            var json = JSON.parse(data)

    var len = json.length
    i = 0;

    var options = {
   title: {
        text: []
        },
        xAxis: {
            categories: []
        },
        series: []
    }

        options.title.text = json.title.text

    for (i; i < len; i++) {
        if (i === 0) {
            var dat = json[i].data,
                lenJ = dat.length,
                j = 0,
                tmp;

            for (j; j < lenJ; j++) {
                tmp = dat[j].split(';');
                options.xAxis.categories.push(tmp[0]);
            }
        } else {
                options.series.push(json[i]);
        }
    }



    $('#container').highcharts('StockChart', options);

        },
    error: function(jqXHR, textStatus, errorThrown)
    {
    }
});

}
8
  • There's no such thing as a "JSON Object". Why don't you use jQuery's functionality to create a query string? Now you'll have to use encodeURIComponent to properly encode query string parameters. And you can also use 'json' as last parameter of $.post, so you don't have to manually use JSON.parse, which might not be available. Commented Nov 4, 2013 at 15:42
  • Did you try console.log on the response data? It’s usually very easy to fond the structure from there. Commented Nov 4, 2013 at 15:42
  • your json structure is not built in a way that can be easily iterated over. The meta data should be separated from the results. That or you don't need to iterate over it at all. Commented Nov 4, 2013 at 15:50
  • David, thanks for your answer. Not sure what console.log does, but the "output" ajax response data is as follows: [{"title":{"text":"idiot"},"data":["2012-12-16; 0","2012-12-16; 23"]},{"name":"Sacred cows","data":[98.9914,99.5429]}] Commented Nov 4, 2013 at 15:50
  • json.title.text is wrong. Given the array of objects you just posted, how do you think you should access the title property of the first object in the array? Commented Nov 4, 2013 at 15:51

1 Answer 1

3

The way you are reading the parsed json is incorrect. I was able to access the title when I tried json[i].title.text.

The array index is missing in the line : options.title.text = json.title.text. You can try options.title.text = json[0].title.text instead.

<script>
  var json = JSON.parse('[{"title":{"text":"idiot"},"data":["2012-12-16; 0","2012$


  for (var i = 0; i < json.length; i++) {
     document.write(json[i].title.text); 
  }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you to Kevin B and Rayhan for their answers to this question

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.