duanpie2414 2013-11-04 15:38
浏览 73
已采纳

从JSON数组中提取JSON对象

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)
    {
    }
});

}
  • 写回答

1条回答 默认 最新

  • douqian1296 2013-11-04 16:05
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答