简体   繁体   English

从JSON数组中提取JSON对象

[英]Pulling JSON object from JSON array

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. 我正在尝试通过JSON加载图表,并且JSON是由PHP数组创建的(其原因是我正在从MySQL检索数据。)该系列加载良好,但是我无法提取图表标题。 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. 我的理解是,该代码必须类似于json.title.text ...,但这无法正常工作并使脚本崩溃。 Any help would be greatly appreciated! 任何帮助将不胜感激!

The PHP array is as follows (I have put dummy variable to simplify...): PHP数组如下(为简化起见,我放置了虚拟变量...):

    $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: 生成图表的javascript如下:

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

}

The way you are reading the parsed json is incorrect. 您读取解析的json的方式不正确。 I was able to access the title when I tried json[i].title.text . 我尝试json[i].title.text时可以访问标题。

The array index is missing in the line : options.title.text = json.title.text . 该行中缺少数组索引: options.title.text = json.title.text You can try options.title.text = json[0].title.text instead. 您可以尝试使用options.title.text = json[0].title.text代替。

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM