多个JSON对象响应

时间:2012-08-21 10:03:50

标签: php javascript ajax google-visualization

我有一种情况,我在php文件'A'中有一个下拉菜单。当我从该下拉列表中选择一个值时,会调用ajax调用并触发另一个php文件B. 在那个文件中,我做数据库提取,并形成两个json对象。这两个json对象是我需要绘制2个不同的数据表和2个不同的图表。

  1. 当我在'B'中回显一个json对象时,我将其作为对'A'中ajax调用的响应

  2. 如果我在B中回显两个json对象,我甚至无法得到回复。

  3. 对于单个json对象响应,我可以绘制datatable并可以使用javascript操作json对象并希望绘制图表

  4. 请告知如何处理这种情况 文件B中的代码

            $json = json_encode($tot_categ);
            $json_percent = json_encode($tot_que_percent);
                $cols_percent = array(
                    array( 'id' => 't', 'label' => 'Title', 'type' => 'string'),
                    array( 'id' => 'n', 'label' => 'Neutral(+) ', 'type' => 'string'),
                    array('id' => 'a', 'label' => 'Agree', 'type' => 'string'),
                    array('id' => 'ne', 'label' => 'Neutral(-)', 'type' => 'string'),
                    array('id' => 'd', 'label' => 'Disagree', 'type' => 'string'),
    
            );
    
            $jcols_percent = json_encode($cols_percent);
            //JSON format accepted by Google tables
            $r_percent= "{cols:".$jcols_percent.','."rows:".$json_percent."}";
            //echo (JSON.stringify($r_percent));
            // echo $r_percent;
    
            $cols = array(
                    array( 'id' => 't', 'label' => 'Title', 'type' => 'string'),
                    array( 'id' => 'l', 'label' => 'Avg ', 'type' => 'string'),
                    array('id' => 'lb', 'label' => 'High', 'type' => 'string'),
                    array('id' => 'lo', 'label' => 'Low', 'type' => 'string')
            );
    
            $jcols = json_encode($cols);
            //JSON format accepted by Google tables
            $r = "{cols:".$jcols.','."rows:".$json."}";
            //echo(json_decode($r));
            echo $r;
    

    $ r和$ r_percent是我的对象

    回显时

    $ r_percent给出

    {cols:[{"id":"t","label":"Title","type":"string"},{"id":"n","label":"Neutral(+) ","type":"string"},{"id":"a","label":"Agree","type":"string"},{"id":"ne","label":"Neutral(-)","type":"string"},{"id":"d","label":"Disagree","type":"string"}],rows:[{"c":[{"v":"165q"},{"v":0},{"v":0},{"v":0},{"v":100}]},{"c":[{"v":"160q"},{"v":0},{"v":0},{"v":0},{"v":6}]},{"c":[{"v":"161q"},{"v":0},{"v":0},{"v":0},{"v":10}]},{"c":[{"v":"162q"},{"v":7},{"v":0},{"v":7},{"v":0}]},{"c":[{"v":"163q"},{"v":0},{"v":25},{"v":0},{"v":0}]},{"c":[{"v":"164q"},{"v":0},{"v":100},{"v":0},{"v":0}]}]}
    
    当回显给出

    时,

    $ r

    {cols:[{"id":"t","label":"Title","type":"string"},{"id":"l","label":"Avg ","type":"string"},{"id":"lb","label":"High","type":"string"},{"id":"lo","label":"Low","type":"string"}],rows:[{"c":[{"v":"165q"},{"v":1.3333333333333},{"v":"2"},{"v":"1"}]},{"c":[{"v":"160q"},{"v":6},{"v":"10"},{"v":"1"}]},{"c":[{"v":"161q"},{"v":6.6666666666667},{"v":"9"},{"v":"2"}]},{"c":[{"v":"162q"},{"v":7},{"v":"9"},{"v":"3"}]},{"c":[{"v":"163q"},{"v":8},{"v":"9"},{"v":"6"}]},{"c":[{"v":"164q"},{"v":5},{"v":"5"},{"v":"5"}]}]}
    

    合并时

    $result = array('objA' => $r_percent, 'objB' => $r );
            echo json_encode($result);
    

    更新了回音

    {"objA":"{cols:[{\"id\":\"t\",\"label\":\"Title\",\"type\":\"string\"},{\"id\":\"n\",\"label\":\"Neutral(+) \",\"type\":\"string\"},{\"id\":\"a\",\"label\":\"Agree\",\"type\":\"string\"},{\"id\":\"ne\",\"label\":\"Neutral(-)\",\"type\":\"string\"},{\"id\":\"d\",\"label\":\"Disagree\",\"type\":\"string\"}],rows:[{\"c\":[{\"v\":\"165q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":100}]},{\"c\":[{\"v\":\"160q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":6}]},{\"c\":[{\"v\":\"161q\"},{\"v\":0},{\"v\":0},{\"v\":0},{\"v\":10}]},{\"c\":[{\"v\":\"162q\"},{\"v\":7},{\"v\":0},{\"v\":7},{\"v\":0}]},{\"c\":[{\"v\":\"163q\"},{\"v\":0},{\"v\":25},{\"v\":0},{\"v\":0}]},{\"c\":[{\"v\":\"164q\"},{\"v\":0},{\"v\":100},{\"v\":0},{\"v\":0}]}]}","objB":"{cols:[{\"id\":\"t\",\"label\":\"Title\",\"type\":\"string\"},{\"id\":\"l\",\"label\":\"Avg \",\"type\":\"string\"},{\"id\":\"lb\",\"label\":\"High\",\"type\":\"string\"},{\"id\":\"lo\",\"label\":\"Low\",\"type\":\"string\"}],rows:[{\"c\":[{\"v\":\"165q\"},{\"v\":1.3333333333333},{\"v\":\"2\"},{\"v\":\"1\"}]},{\"c\":[{\"v\":\"160q\"},{\"v\":6},{\"v\":\"10\"},{\"v\":\"1\"}]},{\"c\":[{\"v\":\"161q\"},{\"v\":6.6666666666667},{\"v\":\"9\"},{\"v\":\"2\"}]},{\"c\":[{\"v\":\"162q\"},{\"v\":7},{\"v\":\"9\"},{\"v\":\"3\"}]},{\"c\":[{\"v\":\"163q\"},{\"v\":8},{\"v\":\"9\"},{\"v\":\"6\"}]},{\"c\":[{\"v\":\"164q\"},{\"v\":5},{\"v\":\"5\"},{\"v\":\"5\"}]}]}"}
    

1 个答案:

答案 0 :(得分:7)

将两个响应组合在一个对象中,返回该对象并再次单独使用这两个部分:

PHP方面:

$result = array( 'objA' => $objA, 'objB' => $objB );
echo json_encode( $result );

客户方:

// your success callback handler
function handler( data ) {
  // execute code for object A
  doStuff( data.objA );
  // execute code for object B
  doOtherStuff( data.objB );
}