多个JSON对象响应 [英] multiple JSON object response

查看:75
本文介绍了多个JSON对象响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a situation where i have a drop down in php file 'A'.and when i select a value from that drop down an ajax call is made and a different php file B is triggered. In that file,i do db fetch,and form two json objects.these two json objects are required for me to draw 2 different data tables and 2 different charts.


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

  1. when i echo one json object in 'B' i get that as a response to ajax call in 'A'

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

I cant even get a response if i echo two json objects in B.

对于单个json对象响应,我可以绘制数据表并且可以用javascript操纵json对象并希望绘制图表

for a single json object response,i can draw datatable and can manipulate json object with javascript and hopefully draw chart

请告知如何处理这种情况
来自文件的代码B

Please advise how to handle this situation Code from file 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 and $r_percent are my objects

$ r_percent当echoed给出

$r_percent when echoed gives

{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当echoed给出

$r when echoed gives

{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\"}]}]}"}


推荐答案

将两个答案结合起来单个对象,返回该对象并再次单独使用这两个部分:

Combine both responses in a single object, return that object and use both parts separately again:

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

这篇关于多个JSON对象响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆